Convert JPG image from grayscale to RGB using imagemagick

15,034

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
Share:
15,034

Related videos on Youtube

NotYanka
Author by

NotYanka

Updated on September 15, 2022

Comments

  • NotYanka
    NotYanka over 1 year

    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
    
  • NotYanka
    NotYanka about 8 years
    Thanks a lot for the quick response. For the record: convert input.jpg -type truecolor result.jpg worked fine. While adding -colorspace sRGB yielded the same result, using it by itself did not get the job done.
  • Shayan
    Shayan about 4 years
    Doesn't work for a PNG file, I tried:-colorspace sRGB -type truecoloralpha my image is 8 bits and I don't want to convert it to PNG24.
  • Mark Setchell
    Mark Setchell about 4 years
    @Shayan If you have a question, please ask it in the usual way by posting a question along with your image.
  • Alexis Wilke
    Alexis Wilke almost 3 years
    @Shayan According to this post on the ImageMagick forum, you also have to use -define colorspace:auto-grayscale=off. I would imagine that the detection for grayscale is applied at the time you save and whatever conversion you had before that won't force the colorspace one way or the other.
  • fmw42
    fmw42 over 2 years
    With PNG, you have to specify the prefix PNG24:filename.png to convert from grayscale 8-bit to color 24-bit.