Command line 使用imagemagick将JPG图像从灰度转换为RGB

Command line 使用imagemagick将JPG图像从灰度转换为RGB,command-line,imagemagick,jpeg,rgb,grayscale,Command Line,Imagemagick,Jpeg,Rgb,Grayscale,我正在尝试使用 对于PNG图像,它可以正常工作,使用: convert image.png -define png:color-type=2 result.png (摘自对的回答) 尽管使用identification-format%r result.png检查仍将返回DirectClass灰色,但我可以看到它使用gdalinfo工作,因为现在列出了3个波段/频道: gdalinfo[已成功转换PNG]: Driver: PNG/Portable Network Graphics Files:

我正在尝试使用

对于PNG图像,它可以正常工作,使用:

convert image.png -define png:color-type=2 result.png
(摘自对的回答)

尽管使用
identification-format%r result.png检查仍将返回DirectClass灰色,但我可以看到它使用
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
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
然而,它似乎只适用于PNG图像

我的问题:我如何为JPG图像实现相同的效果

当我为JPG尝试上述命令时,它不起作用:

convert image.jpg -define jpg:color-type=2 result.jpg
gdalinfo[未成功转换JPG]:

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
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

PNG颜色类型不适用于JPEG图像,因此不能使用相同的技术。尝试将颜色空间强制设置为sRGB,和/或将类型设置为TrueColour,这样您就不会得到苍白的图像:

convert input.jpg -colorspace sRGB -type truecolor result.jpg

非常感谢您的快速回复。记录如下:
convert input.jpg-type truecolor result.jpg
工作正常。当添加
-colorspace sRGB
产生相同的结果时,单独使用它并不能完成任务。对于PNG文件不起作用,我尝试了:
-colorspace sRGB-type truecoloralpha
我的图像是8位的,我不想将其转换为PNG24。@Shayan如果你有问题,请用通常的方式提问,在你的图片中附上一个问题。@Shayan根据,你还必须使用
-定义颜色空间:auto grayscale=off
。我可以想象,灰度检测是在保存时应用的,之前进行的任何转换都不会以任何方式强制颜色空间。