0

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

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

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

ETC...

有什么办法可以将其转换为图像,例如 bmp 文件或其他光栅格式。

4

2 回答 2

2

也许你可以用 NetPBM 的格式写出你的图像,这种PPM格式非常简单,并且在 Wikipedia 上有文档

因此,例如以下(放大的)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
于 2018-02-17T09:19:34.070 回答
1

基于 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 计算正确的行数。那就是文件必须以换行符结尾。

于 2018-02-17T18:26:38.293 回答