Search code examples
imagemagickimagemagick-convert

How to force imagemagick to keep the original colorspace of an image


I have a problem using imagemagicks convert and keeping the colorspace.

I have a JPEG that is just black (this is an edge case). When I run identify on it this is the output:

black.jpg JPEG 1920x1080 1920x1080+0+0 8-bit sRGB 12787B 0.000u 0:00.000

I then add some text to the image:

convert black.jpg -pointsize 50 -fill WHITE -draw "text 100,100 'foobar'" test.jpg

Now running identify on the resulting test image shows:

test.jpg JPEG 1920x1080 1920x1080+0+0 8-bit Gray 256c 11316B 0.000u 0:00.000

I tried to add the -colorspace sRGB option but it did not help at all. No matter what I do, the colorspace is always converted from sRGB to Gray.

How can I tell imagemagick to leave the colorspace as it is in the original image?

ImageMagick 6.9.10-23 Q16 arm 20190101


Solution

  • In Imagemagick, for most formats you can just add -type truecolor. For example:

    convert black.jpg -pointsize 50 -fill WHITE -draw "text 100,100 'foobar'" -type truecolor test.jpg
    


    should do what you want.

    Note for PNG you need to add PNG24: to preface the output as PNG24:test.png