通过一次转换调用读取图像数据和元数据

时间:2014-06-24 15:50:25

标签: imagemagick imagemagick-convert

我正在开发一个图像处理工具,我目前正在子进程中使用imagemagick来解压缩(和压缩)图像。这很好用,但看起来我必须进行两个 convert的并行调用才能获得我需要的所有数据:

convert jpeg:- rgb:-  # To read the image data
convert jpeg:- info:- # To read metadata, such as the dimensions of the image

图像的尺寸对于理解使用rgb获得的原始图像数据当然是必不可少的。

这似乎很浪费。理想情况下,我希望通过一次调用convert来获取这两种数据,因此源图像只需要处理一次。可能的解决方案可能是:

  • 在将convert数据写入另一个时,rgbinfo数据写入一个文件描述符的方法,两者都基于相同的输入图像。我似乎无法找到如何做到这一点,但convert之前让我惊讶于它的多功能性。任何的想法? (类似于convert jpeg:- info:fd:2 rgb:-,但这被解释为两个输入,而不是两个输出)

  • 某些输出格式与rgb一样易于阅读(来自node.js),但包含图像尺寸。我认为bmp对于这个任务太毛茸茸了。我应该考虑另一种格式吗? list of supported formats很长,所以这里的任何提示都会有所帮助:)

2 个答案:

答案 0 :(得分:1)

如何使用PNM格式,它非常简单并且记录在案here

convert jpeg:- pnm:-

如果您使用-compress none,您将获得图像数据的P1,P2或P3和ASCII输出,而如果您不这样做,您将获得具有ASCII标头信息和二进制信息的格式P4,P5和P6 (快!)像素数据。

P3
# The P3 means colors are in ASCII, then 3 columns and 2 rows, 
# then 255 for max color, then RGB triplets 
3 2
255
255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0

示例:

# convert rose: -compress none image.pbm
# P1
# 70 46
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
# 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 
# 
# convert rose: -compress none image.pgm
# P2
# 70 46
# 255
# 47 48 51 52 52 51 49 50 49 49 48 46 45 46 46 45 46 49 52 54 58 64 67 66 67 68 
# 77 84 89 96 101 103 101 99 96 88 86 86 84 81 79 78 78 78 80 81 89 92 86 62 54 
# 
# convert rose: -compress none image.ppm
# P3
# 70 46
# 255
# 48 47 45 50 48 46 54 50 47 56 51 46 58 51 45 57 50 45 56 48 45 57 49 46 
# 56 48 45 56 48 45 55 47 44 53 45 42 52 44 41 53 45 42 53 45 42 49 45 39 
# 
# convert rose: image.pbm
# P4
# 70 46
# 
# convert rose: image.pgm
# P5
# 70 46
# 255
# 
# convert rose: image.ppm
# P6
# 70 46
# 255

答案 1 :(得分:1)

使用fd:文件处理程序将输出重定向到不同的文件句柄。

convert jpeg:- -write info:fd:2 rgb:fd:1

info:将在stderr中,rgb:将在stdout中。使用基本的I/O redirection来处理。