I'm using ImageMagick 7.1.1-8 and just had the same issue:
$ magick example.pdf output.png
magick: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `output.png' @ warning/png.c/MagickPNGWarningHandler/1526.
$ magick -version
Version: ImageMagick 7.1.1-8 Q16-HDRI x86_64 d92cb0e65:20230421 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5)
Delegates (built-in): bzlib djvu fontconfig freetype heic jbig jng jpeg lcms lqr lzma openexr png raqm tiff webp x xml zlib
Compiler: gcc (7.5)
$
In my case, ImageMagick (using Ghostscript) is reading my input PDF file and produces image data in sRGB colorspace with an embedded ICC profile:
$ magick example.pdf -verbose info: | grep "Colorspace:"
Colorspace: sRGB
$ magick example.pdf -verbose info: | grep -A1 "Profiles:"
Profiles:
Profile-icc: 2576 bytes
$
When saving that image data as a PNG file, ImageMagick automatically optimizes the colorspace and saves the file as a grayscale PNG with color type 0:
$ magick output.png -verbose info: | grep "Colorspace:"
Colorspace: Gray
$ magick output.png -verbose info: | grep "color_type"
png:IHDR.color_type: 0 (Grayscale)
$
But it does not remove the embedded ICC profile:
$ magick output.png -verbose info: | grep -A1 "Profiles:"
Profiles:
Profile-icc: 2576 bytes
$
That's what the warning is about: libpng is not expecting a color ICC profile embedded in a grayscale PNG.
To get rid of the warning, we can either
- keep the ICC profile but hide warnings in general,
- remove the ICC profile or
- keep the ICC profile and save not as a grayscale PNG.
Keep the ICC profile but hide warnings in general
If you want to simply hide the warning, use the -quiet
option. After all, it's not an error, but only a warning:
$ magick example.pdf -quiet output.png
$
A downside might be that if other warnings show up in the future, you won't notice because you are already hiding them, too.
Remove the ICC profile
Instead, you can choose to remove the ICC profile. There's multiple ways to do so. Other answers mention changing the colorspace, for example. This (sometimes) works, because ImageMagick automatically removes embedded profiles when changing the colorspace:
(void) DeleteImageProfile(image,"icc");
(void) DeleteImageProfile(image,"icm");
Source: https://github.com/ImageMagick/ImageMagick/blob/7a63f554dc0f986aa3ef2767d041a0f3294decd2/MagickCore/colorspace.c#L1624-L1625
There's two disadvantages to this approach:
If your image is already in colorspace sRGB and you use -colorspace sRGB
to convert to colorspace sRGB, ImageMagick ignores the operator and therefore the profile will not get removed. For example, my input PDF file produced an image in sRGB colorspace which means -colorspace sRGB
has no effect and does not remove the profile:
$ magick example.pdf -colorspace sRGB -verbose info: | grep -A1 "Profiles:"
Profiles:
Profile-icc: 2576 bytes
$
Conversely, the same is true if you use -colorspace RGB
on an image that is already in RGB colorspace. So if you want to use this method to remove the ICC profile, you would have to check in advance if your image is in sRGB or RGB and then use -colorspace
accordingly.
Obviously, if you use -colorspace
to change the colorspace of your image, the colors of your image might change which makes the image look different. Therefore, after converting the colorspace to a different colorspace to remove the profile, you should convert the colorspace back to the original colorspace. So your command will use the -colorspace
operator twice. For my example file, which is sRGB, I convert to RGB first, then back to sRGB:
$ magick example.pdf -colorspace RGB -colorspace sRGB -verbose info: | grep "Colorspace:"
Colorspace: sRGB
$ magick example.pdf -colorspace RGB -colorspace sRGB -verbose info: | grep -A1 "Profiles:"
$
You will probably still get some rounding errors from the conversions, but the image will more or less look identical.
An alternative way to remove the profile is to simply use -strip
:
$ magick example.pdf -strip -verbose info: | grep -A1 "Profiles:"
$
However, that option not only removes all profiles but also further metadata, which might not be desired behavior. See https://imagemagick.org/script/command-line-options.php#strip for more information.
To specifically remove the ICC profile from the image before saving, you can use +profile icc
:
$ magick example.pdf +profile icc -verbose info: | grep -A1 "Profiles:"
$
Alternatively, to specifically not read any ICC profile from the input file in the first place, you can use -define profile:skip=icc
:
$ magick -define profile:skip=icc example.pdf -verbose info: | grep -A1 "Profiles:"
$
Note that this setting affects reading, so you have to put it before your input file.
Keep the ICC profile and save not as a grayscale PNG
Finally, if you want to keep the profile, you can use -define png:color-type=
to set your desired PNG color type to avoid automatic conversion to grayscale and therefore to avoid the warning. For my example file, I might want "Truecolour with alpha" which is color type 6:
$ magick example.pdf -define png:color-type=6 output.png
$ magick output.png -verbose info: | grep "color_type"
png:IHDR.color_type: 6 (RGBA)
$
Alternatively, you can use for example the PNG32:
output prefix:
$ magick example.pdf PNG32:output.png
$ magick output.png -verbose info: | grep "color_type"
png:IHDR.color_type: 6 (RGBA)
$
In both cases, no warning shows up, yet the ICC profile is retained:
$ magick output.png -verbose info: | grep -A1 "Profiles:"
Profiles:
Profile-icc: 2576 bytes
$
You can read further about this issue at ImageMagick's GitHub Discussions page at https://github.com/ImageMagick/ImageMagick/discussions/2730 and https://github.com/ImageMagick/ImageMagick/discussions/6292 which provides excellent support.