Convert JPG image from grayscale to RGB using imagemagick

扶醉桌前 提交于 2020-01-01 08:05:23

问题


I am trying to convert gray scale images to RGB using the imagemagick command-line tools.

It is working fine for PNG images, using:

convert image.png -define png:color-type=2 result.png

(taken from an answer to "How to convert gray scale png image to RGB from comand line using image magick")

Although checking with identify -format %r result.png will still return DirectClass Gray, I can see it worked using gdalinfo as there are now 3 bands / channels listed:

gdalinfo [successfully converted 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

However, it seems the -define option is only working for PNG images.

My question: How can I achieve the same effect for JPG images?

When I try the above command for JPG, it does not work:

convert image.jpg -define jpg:color-type=2 result.jpg

gdalinfo [unsuccessfully converted 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

回答1:


The PNG colour types do not apply to JPEG images, so you can't use the same technique. Try forcing the colorspace to sRGB, and/or setting the type to TrueColour so you don't get a palettised image:

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


来源:https://stackoverflow.com/questions/34874771/convert-jpg-image-from-grayscale-to-rgb-using-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!