Search code examples
imagemagickmogrify

How to convert in-place batch of PNG from grayscale to RGB?


So, I want to convert many PNGs from grayscale to RGB, similar to this question

However, the given command there does not work for PNGs. Moreover, that is convert and I read that to process images in-place one would have to use mogrify. How can I use this?


Solution

  • So, this is both a bug and a feature of imagemagic. To quote the feature part:

    It turns out ImageMagick tries to set to PNG8 based on image content even if the file was previously PNG24 or PNG32

    Therefore, if you want to end up with 3 channels per image, you have to set png24 format (3 channels x 8 bits/channel).

    So the solution is:

    # for a single image, with a different output
    convert input.png -type TrueColor png24:output.png
    
    # for a batch of images, IN_PLACE !!! BE VERY CAREFUL 
    mogrify -define png:format=png24 -type TrueColor *.png
    

    It's mandatory to realise that mogrify will modify them in place so make sure this is what you want, as there is no way to recover the original images.