Unix命令将文本像素描述转换为和从通用图像文件类型

发布于 2025-02-09 09:57:34 字数 169 浏览 2 评论 0原文

回到几十年后,我记得在Solaris系统上绊倒在一个命令家族中,这些命令将图像文件(JPEG,BMP等)转换为文本形式,将图像描述为像素数组和像素上的一行文本上的像素和像素红色,绿色,蓝色为ASCII数字。这样,您可以轻松地创建,处理和修改任何语言的图像,而不会在图像文件规范中陷入困境。

有人可以给我带头吗?

Back a few decades, I recall on a Solaris system stumbling across a family of commands that convert an image file (jpeg, bmp, etc.) to and from a textual form that described images as pixel arrays and pixels on one line of text each with the red, green, blue as ascii digits. With this, you could easily create, process, and modify images in any language without getting bogged down in the image file specifications.

Can someone give me a lead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2025-02-16 09:57:34

您可能需要从 netpbm 家族中查看ppm格式。

至于命令行工具,除了具有代码的工具外, imagemagick 格式很好。只需使用转换命令。

NETPBM格式不会进行任何压缩,对于给定的像素数据,转换器会产生一个均匀的文件。我在测试套件中成功使用了此功能,确保软件生成的图像具有所需的内容 - 请参见示例(这是GO,但可以使用NetBPM绑定)。

You may want to have a look at the PPM format from the Netpbm family.

As for the command line tools, apart from the ones coming with the code, ImageMagick handles the Netpbm formats very well. Just use convert command.

Netpbm formats don't do any compression and the converters, for a given pixel data, produce an uniform file. I successfully use this feature in test suites, making sure that the image generated by the software has the desired content - see the example (it is Go, but could be anything with Netbpm bindings).

快乐很简单 2025-02-16 09:57:34

正如@wojciechkaczmarek所说,使用NetPBM。如果您想要一个具有 ImageMagick 的具体示例,则需要使用NetPBM文件的ASCII版本(即P1,P2和P3)使用-compress none

因此,用一个黑色,一个白色和一个石灰绿色像素制作3像素宽的图像:

magick xc:black xc:white xc:lime +append image.png

上尺度的输出 - 您可以看到它:

现在转换为ASCII PPM ,#之后的所有内容都是我的评论:

magick image.png -compress none ppm:              
P3                            # ASCII PPM signature
3 1                           # dimensions are 3x1 pixels
255                           # pixels are 8-bit scaled
0 0 0 255 255 255 0 255 0     # black pixel, white pixel, lime green pixel

上面的语法是在stdout上编写ppm文件。如果要将其写入文件,只需做:

magick image.png -compress none result.ppm

显然它也适用于BMP和所有其他格式:

magick image.bmp -compress none result.ppm

您使用各种工具完成处理图像时,您可以使用:

magick image.ppm result.bmp

当 使用 ImageMagick 创建一个小png图像,我可以向您展示如何使用 netpbm 工具将其转换为ASCII

pngtopam -plain image.png
P3
3 1
255
0 0 0 255 255 255 0 255 0 

PPM代码我较早显示magick image.png -compress无PPM:

As @WojciechKaczmarek says, use NetPBM. If you want a concrete example with ImageMagick you need to use -compress none for the ASCII versions (i.e. P1, P2 and P3) of NetPBM files.

So, make a 3-pixel wide image with one black, one white and one lime green pixel:

magick xc:black xc:white xc:lime +append image.png

Upscaled Output - so you can see it:

enter image description here

Now convert to ASCII PPM, everything after the # is my comment:

magick image.png -compress none ppm:              
P3                            # ASCII PPM signature
3 1                           # dimensions are 3x1 pixels
255                           # pixels are 8-bit scaled
0 0 0 255 255 255 0 255 0     # black pixel, white pixel, lime green pixel

The above syntax is for writing the PPM file on stdout. If you want it written to a file, just do:

magick image.png -compress none result.ppm

Obviously it works for BMPs and all other formats too:

magick image.bmp -compress none result.ppm

When you have finished processing your image with assorted tools, you can convert back to a BMP or other format with:

magick image.ppm result.bmp

Now that we have created a little PNG image with ImageMagick, I can show you how to convert it to ASCII PPM with the NetPBM tools:

pngtopam -plain image.png
P3
3 1
255
0 0 0 255 255 255 0 255 0 

Hopefully you will see that this gives the same result as the code I showed earlier magick image.png -compress none ppm:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文