【问题标题】:How to turn a csv(or similar) to bmp(or similar)如何将 csv(或类似)转换为 bmp(或类似)
【发布时间】:2018-02-17 07:39:39
【问题描述】:

我目前正在 Microsoft Small Basic 中做一个项目来获得一些乐趣,但我遇到了困难。

我有一个可以导出到任何格式的文件的数组,它使用字节作为其最小的东西,例如 csv。 每个像素都是一个十六进制值,FFFFFF 说,它被放入一个文件中,如下所示:

FFFFFF,000000,FFF000,000FFF
000AAA,AAAAAA,AAA000,000000

等等……

有什么办法可以把它变成图像,例如 bmp 文件或其他光栅格式。

【问题讨论】:

  • 不多——我在网上看了看 csv 到 bmp 的转换器,但实际上没有。
  • 所以您有一个特定任务,而您的研究包括在谷歌上搜索该特定任务的解决方案?为什么不将其分解为可管理的部分,例如 (1) 读取 CSV,(2) 创建 BMP?

标签: csv bmp basic smallbasic


【解决方案1】:

也许您可以用 NetPBM 的 PPM 格式写出您的图像,这种格式非常简单,并记录在 Wikipedia here 上。

因此,例如以下(放大的)3x2 图像:

看起来像这样(每行 # 后面的部分是我的评论):

P3                               # P3 means ASCII, 3-channel RGB
3 2                              # width=3, height=2
255                              # MAX=255, therefore 8-bit
255 0 0 0 255 0 0 0 255          # top row of pixels in RGB order
0 255 255 255 0 255 255 255 0    # bottom row of pixels in RGB order

然后您可以使用 ImageMagick,它安装在大多数 Linux 发行版上,可用于 macOS 和 Windows,在命令行中将其制作成 BMP,如下所示:

magick input.ppm output.bmp

或者,如果您想要具有对比度拉伸并调整为 800x600 的 JPEG:

magick input.ppm -resize 800x600 -auto-level output.jpg

您同样可以使用 GIMPAdobe Photoshop、可能是 MS Paint、可能是 IrfanView 进行转换,或使用 NetPBM 工具包。例如,使用 NetPBM 工具(比 ImageMagick重量轻得多),转换将是:

ppmtobmp image.ppm > result.bmp

【讨论】:

    【解决方案2】:

    在 Mark 的优秀答案的基础上,使用 ImageMagick 和 Unix 脚本,可以执行以下操作:

    Convert your text file so as to replace commas with new lines, then add leading # to your hex values and store in an array (arr)
    
    Then change each hex value into colors as rgb triplets in the range 0-255 integers with spaces between the 3 values and put into a new array (colors).
    
    Find out how many rows and columns you have in your text file.
    
    Then convert the array of colors into a PPM image and convert that image to bmp while enlarging to 300x200.
    


    下面是对应的代码:

    arr=()
    colors=()
    arr=(`cat test.txt | tr "," "\n" | sed 's/^/#/'`)
    num=${#arr[*]}
    for ((i=0; i<num; i++)); do
    colors[i]=`convert xc:"${arr[$i]}" -format "%[fx:round(255*u.r)] %[fx:round(255*u.g)] %[fx:round(255*u.b)]" info:`
    done
    numrows=`cat test.txt | wc -l`
    numvalues=`cat test.txt | tr "," " " | wc -w`
    numcols=`echo "scale=0; $values/$rows" | bc`
    echo "P3 $numcols $numrows 255 ${colors[*]}" | convert - -scale 400x200 result.bmp
    

    注意:我必须在最后一个颜色字符之后在文本文件中添加一个新行,以便 wc -l 计算正确的行数。即文件必须以换行符结尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      相关资源
      最近更新 更多