[SOLVED] PNG to ICO while preserving transparency

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
rzn1986
Posts: 2
Joined: 2014-09-16T06:24:50-07:00
Authentication code: 6789

[SOLVED] PNG to ICO while preserving transparency

Post by rzn1986 »

Hello,

I'm trying to convert a PNG file to a ICO file (favicon) while keeping the PNG transparency intact. I'm doing this with the sample code provided at http://www.imagemagick.org/Usage/thumbnails/#favicon.

This is my source image, a PNG24 file with transparency: http://www.feitjevandedag.nl/logo.png

This is the code I'm using:

Code: Select all

"C:\Program Files\ImageMagick\convert.exe" "C:\Program Files\ImageMagick\logo.png" -bordercolor white -border 0 ^ 
^( -clone 0 -resize 16x16 ^) ^ 
^( -clone 0 -resize 32x32 ^) ^ 
^( -clone 0 -resize 48x48 ^) ^ 
^( -clone 0 -resize 64x64 ^) ^ 
-delete 0 -background none favicon.ico
The problem is that the background is always white.

I've tried a lot of parameters like "-alpha set", "-alpha background" and tried it without the "-background none" but the background always remains white, setting "-alpha copy" actually makes the black parts transparent. Looking around the web, people claim removing the "-alpha off" from the sample code mentioned above should do the trick but I haven't gotten it to work yet.

I'm using ImageMagick-6.8.9-7-Q16-x64-dll (for Windows).

Any help is appreciated. :D
Last edited by rzn1986 on 2014-09-16T07:32:19-07:00, edited 1 time in total.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: PNG to ICO while preserving transparency

Post by dlemstra »

Why do you need the '-bordercolor white -border 0' option? Removing this parameters seems to fix the problem. And in nieuwere versies of ImageMagick you can do what you are trying to do much easier:

Code: Select all

convert logo.png -define icon:auto-resize=64,48,32,16 logo.ico
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
rzn1986
Posts: 2
Joined: 2014-09-16T06:24:50-07:00
Authentication code: 6789

Re: PNG to ICO while preserving transparency

Post by rzn1986 »

Thanks a lot dlemstra! :D

I didn't think that would help, too bad I didn't try it. I'm new to ImageMagick so still need to learn a lot, the code you posted works perfectly and is a lot easier.

You got some Dutch in between the English btw, "nieuwere versies", good thing I am Dutch. :lol:
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: [SOLVED] PNG to ICO while preserving transparency

Post by dlemstra »

That was added by accident :P
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
AlexMorse2016
Posts: 2
Joined: 2016-01-07T10:17:09-07:00
Authentication code: 1151

Re: [SOLVED] PNG to ICO while preserving transparency

Post by AlexMorse2016 »

This does preserve the transparency (from png with alpha trans), but when I add -color 8, to reduce colors, the background color comes back and the image is degraded badly. Is there a way to also reduce .ico file size when converting form a png? The PNG is 300bytes, but when exported by ImageMagick its back to 1500bytes. any suggestions?
Post Reply