How to allow ImageMagick to convert a grayscale png to an ico file without errors

image conversionimagemagick

I have a grayscale PNG, and when trying to use ImageMagick (Version 7.1.1-28) to create an ICO file it always gives me the following warning:

$ convert favicon.png -define icon:auto-resize=16,32,48,64,256 favicon.ico
convert: Cannot write image with defined png:bit-depth or png:color-type. `' @ warning/png.c/MagickPNGWarningHandler/1526.

I thought it might be the color profile of my original PNG so I tried using ImageMagick to force the image (maybe my assumption is incorrect here?) to an RGB colorspace first and then make the ICO file from that intermediate file, but got the exact same error.

$ convert favicon.png +profile "*" -colorspace RGB ico-temp.png
$ convert ico-temp.png -define icon:auto-resize=16,32,48,64,256 favicon.ico
convert: Cannot write image with defined png:bit-depth or png:color-type. `' @ warning/png.c/MagickPNGWarningHandler/1526.

What am I doing incorrectly here? Are there any flags I might be able to add to make this work?


And for anyone who would like to try reproducing, I'm finding that any grayscale SVG or PNG being converted into an ICO file gets this error. This is an example SVG I made to reproduce this (also works if you convert it to a PNG with this source):

<svg xmlns="http://www.w3.org/2000/svg" 
    width="100mm" 
    height="100mm" 
    viewBox="0 0 100 100">
  <circle cx="25" cy="75" r="20" style="fill:#ccc"/>
  <circle cx="65" cy="35" r="30" style="fill:#4d4d4d"/>
</svg>

Best Answer

It think the solution to use -compress none as explained here; tested in my install of ImageMagick 7.1.1-28 on macOS:

convert favicon.png -compress none -define icon:auto-resize=16,32,48,64,256 favicon.ico

When I do that with your example favicon.svg converted to favicon.png, it cleanly creates a favicon.ico without issue.

enter image description here


Other attempts below.

What about this as suggested in this answer:

convert -background transparent "favicon.png" -define icon:auto-resize=16,24,32,48,64,72,96,128,256 "favicon.ico"

Or perhaps the suggestion to use -define profile:skip=ICC as suggested here with -background transparent:

convert -background transparent "favicon.png" -define profile:skip=ICC -define icon:auto-resize=16,24,32,48,64,72,96,128,256 "favicon.ico"

Or without -background transparent:

convert "favicon.png" -define profile:skip=ICC -define icon:auto-resize=16,24,32,48,64,72,96,128,256 "favicon.ico"