我正在尝试使用imagemagick命令行工具将灰度图像转换为RGB。
对于PNG图像,以下命令可以正常运行:
convert image.png -define png:color-type=2 result.png
(取自回答“如何使用ImageMagick在命令行中将灰度png图像转换为RGB”)
虽然使用identify -format %r result.png
仍会返回DirectClass Gray,但我可以看到它已经成功通过gdalinfo
进行了转换,因为现在列出了3个波段/通道:
gdalinfo [成功转换PNG]:
Driver: PNG/Portable Network Graphics
Files: result.png
Size is 567, 479
Coordinate System is `'
Image Structure Metadata:
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 479.0)
Upper Right ( 567.0, 0.0)
Lower Right ( 567.0, 479.0)
Center ( 283.5, 239.5)
Band 1 Block=567x1 Type=Byte, ColorInterp=Red
Band 2 Block=567x1 Type=Byte, ColorInterp=Green
Band 3 Block=567x1 Type=Byte, ColorInterp=Blue
然而,似乎 -define
选项 只适用于PNG图像。
我的问题:我如何在JPG图像中实现相同的效果?
当我尝试对JPG使用上述命令时,它不起作用:
convert image.jpg -define jpg:color-type=2 result.jpg
gdalinfo [未能成功转换JPG]:
Driver: JPEG/JPEG JFIF
Files: result.jpg
Size is 1500, 1061
Coordinate System is `'
Metadata:
EXIF_ColorSpace=1
EXIF_PixelYDimension=2480
...
EXIF_YCbCrPositioning=1
EXIF_YResolution=(300)
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 1061.0)
Upper Right ( 1500.0, 0.0)
Lower Right ( 1500.0, 1061.0)
Center ( 750.0, 530.5)
Band 1 Block=1500x1 Type=Byte, ColorInterp=Gray
Overviews: 750x531, 375x266, 188x133
Image Structure Metadata:
COMPRESSION=JPEG
convert input.jpg -type truecolor result.jpg
运行良好。虽然添加-colorspace sRGB
也产生了相同的结果,但单独使用并不能完成任务。 - NotYanka-colorspace sRGB -type truecoloralpha
,但我的图像是8位的,我不想将其转换为PNG24。 - Shayan-define colorspace:auto-grayscale=off
。我想灰度检测是在保存时应用的,之前的任何转换都不会强制执行颜色空间。@Shayan - Alexis Wilke