如何将RGB值的文本文件转换为png文件? [英] How do I convert a text file of RGB values into a png file?

查看:72
本文介绍了如何将RGB值的文本文件转换为png文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述:

使用ImageMagick,我可以做到:

转换a.png a.txt

这给了我

 #ImageMagick像素枚举:297,239,255,srgba0,0:(255,255,255,1)#FFFFFF白色1,0:(255,255,255,1)#FFFFFF白色2,0:(255,255,255,1)#FFFFFF白色3,0:(255,255,255,1)#FFFFFF白色... 

我还希望能够从文本文件返回到png.我怎样才能做到这一点?我很乐意使用任何linux命令行工具,编程语言库等.

上下文:

我已使用ImageMagick工具将原始图像的像素值解析为q(我正在使用的语言).然后,我对它们进行了转换,现在要保存与新像素值相对应的图像.使用q,我可以轻松地将rgb值保存为所需的任何文本格式.

解决方案

两种方法均相同:

 #图片到文字转换image.png image.txt#文字到图片转换image.txt和Back.png 

虽然最好写一个 NetPBM 文件-所以如果是灰度则写 PGM (P5),如果是彩色则写 PPM (P6).格式非常简单,并在

这是颜色 PPM ,它对应于3x1的RGB像素行:

  P33 1255255 0 00 255 00 0 255 

然后您可以使用 ImageMagick

将其转换回 PNG

 转换image.ppm image.png转换image.pgm image.png 


您还可以按照RGB,RGB,RGB的顺序将原始二进制字节写入文件,然后使用 ImageMagick 将其转换为PNG,如下所示:

 转换-size 1024x768 RGB:data.bin image.png 

或用于灰度:

 转换-size 1024x768灰色:data.bin image.png 

,尽管它会稍微变小,但它不再像PGM/PPM文件那样是自包含的,这意味着您必须单独携带大小才能恢复数据,所以我会选择PGM/PPM版本,因为标题只有3行.


请注意,您还可以使用重量更轻的 NetPBM 工具(或 libvips GIMP )在NetPBM和PNG格式之间进行上述所有转换.strong>或其他),而不是 ImageMagick .

例如,使用 NetPBM PPM 转换为 PNG :

  pnmtopng a.ppm>result.png 

Problem statement:

Using ImageMagick, I can do:

convert a.png a.txt

which gives me:

# ImageMagick pixel enumeration: 297,239,255,srgba
0,0: (255,255,255,1)  #FFFFFF  white
1,0: (255,255,255,1)  #FFFFFF  white
2,0: (255,255,255,1)  #FFFFFF  white
3,0: (255,255,255,1)  #FFFFFF  white
...

I also want to be able to go from a text file back to a png. How can I do this? I'd be happy to use any linux command line tools, programming language libraries etc.

Context:

I have parsed the pixel values of the original image into q (the language I'm using) using the ImageMagick tool. I then performed a transformation on them and now want to save the image corresponding to the new pixel values. Using q I can easily save the rgb values in any text format required.

解决方案

It works the same both ways:

# Image to text
convert image.png image.txt

# Text to image
convert image.txt andBack.png

It is probably better to write a NetPBM file though - so PGM (P5) if greyscale, and PPM (P6) if colour. The format is very simple and described here.

There are several variants known as P1 through P6. P1 is rarely used and just mono and inefficient. P2 is greyscale and ASCII (human-readable) and P5 is the same but binary and therefore more compact. P3 is colour and ASCII (human-readable) and P6 is the same but binary and therefore more compact.

Here is a 4x4 white (255) rectangle with a 1-pixel wide mid-grey (127) border making a 6x6 PGM:

P2
6 6
255
127 127 127 127 127 127 
127 255 255 255 255 127 
127 255 255 255 255 127 
127 255 255 255 255 127 
127 255 255 255 255 127 
127 127 127 127 127 127

Here is a colour PPM which corresponds to a 3x1 row of RGB pixels:

P3
3 1
255
255 0 0
0 255 0
0 0 255

You can then convert this back to PNG with ImageMagick

convert image.ppm image.png
convert image.pgm image.png


You could also just write the raw binary bytes in the order RGB, RGB, RGB to a file and then convert it to PNG with ImageMagick like this:

convert -size 1024x768 RGB:data.bin image.png

or for grayscale:

convert -size 1024x768 GRAY:data.bin image.png

and while that would be marginally smaller, it is no longer self-contained like a PGM/PPM file and means you have to carry around the size separately to be able to get your data back, so I would go for the PGM/PPM version for the sake of 3 tiny lines of header.


Note that you can also do all the above conversions between NetPBM and PNG formats with the much lighter-weight NetPBM tools (or libvips or GIMP, or others) rather than ImageMagick.

For example, with NetPBM, to convert PPM to PNG:

pnmtopng a.ppm > result.png

这篇关于如何将RGB值的文本文件转换为png文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆