Change of color space when converting png images
Did you ever convert png images using ImageMagick's convert
, and suddenly the colors changed? Well, just today I encountered exactly that problem ....
It seems that ImageMagick tries to be too clever, so if it deals with an image that appears to be grayscale, then it changes its colors to grayscale or 256-colors and PseudoClass, even if it was DirectClass before the conversion. This can be detected by using identify
:
$ convert input.png output.png
$ identify input.png
input.png PNG 1920x1080 3840x1080+0+0 8-bit DirectClass 34.4KB 0.000u 0:00.000
$ identify output.png
output.png PNG 1920x1080 3840x1080+0+0 8-bit PseudoClass 256c 18KB 0.000u 0:00.000
I wouldn't mind this behaviour that much, if the colors were looking the same, but no, they changed.
Luckily I found a solution in one of the ImageMagick forums, see http://www.imagemagick.org/discourse-server/viewtopic.php?t=19262.
The trick is to put PNG24 (or PNG32 for images with transparency) in front of the output image name, i.e.
convert input.png PNG24:output.png
Now the color properties are preserved! Note, that this is probably a rare case and won't happen if you have colorful png images to convert. But to be on the save side, it won't hurt to use the PNG24-keyword for png image conversion.