1

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?

Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65

1 Answers1

0

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.

Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65
  • 1
    Since PNG only supports sRGB (no CMYK), adding -colorspace RGB is going to make the image linear color rather than non-linear color sRGB. So unless you are using a very old IM version where RGB and sRGB are swapped, I would remove that. Also proper IM syntax reads a raster input before supplying any settings or operators. So in the convert command read your input png first. – fmw42 Oct 05 '17 at 17:05
  • @fmw42 thank you for advice! Any idea why `mogrify` is so slow ? Takes ~32 seconds for 18 images. If I look in the folder, I see intermediate files being created in the process. Maybe it uses a small memory buffer and if the images are big it uses the disk, despite having memory available – Ciprian Tomoiagă Oct 06 '17 at 07:58
  • Note that it is possible to avoid files being modified in-place by setting `-path /some/where/else`. – Mark Setchell Oct 06 '17 at 09:09
  • Try running `identify -list resource` to see if you have limited memory, for example, available. – Mark Setchell Oct 06 '17 at 09:11
  • @MarkSetchell I have enough free memory on my machine, but maybe IM uses small buffers for the transformations, similar to how `sort` becomes very slow with large files, but can be speedup with `-S` flag – Ciprian Tomoiagă Oct 06 '17 at 09:35
  • Just because you have free memory on your machine does not mean ImageMagick can/will use it. You need to run the command I suggested to see if there are restrictions on ImageMagick. – Mark Setchell Oct 06 '17 at 10:23