57

Is there a command I can use to convert a .jpg or .png or other to extension to a .ico? If possible also to resize it to be a favicon size?

I'd also like to turn it from .ico to .jpg or .png.

Zanna
  • 69,223
  • 56
  • 216
  • 327

3 Answers3

80

The most useful program (suite) to manipulate image is Imagemagick (sudo apt install imagemagick) and for this task you will need the convert binary.

You will need to use something like:

convert -resize x16 -gravity center -crop 16x16+0+0 input.png -flatten -colors 256 -background transparent output/favicon.ico
Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
systho
  • 849
  • 7
  • 6
  • It doesn't keep the transparent section of the image. The alpha channel changed to white. I don't know why. – xdnroot Oct 21 '20 at 12:41
  • 2
    The removal of transparency is most likely due to using the `-flatten` flag. – Andy Oct 28 '20 at 07:40
22

This is the best command to do that from console:

convert <your-image-here> -define icon:auto-resize=256,64,48,32,16 favicon.ico

Hope you like it!

  • 1
    What does the `-define icon:auto-resize=256,64,48,32,16` do? The man pages just says that it `-define` "defines one or more image format options" – Peter T. Nov 01 '21 at 14:49
  • 3
    `icon:auto-resize` to automatically store multiple sizes in an ico image (*requires 256x256 input image*). – rodvlopes Jun 19 '22 at 14:13
  • In my case, this command handled also the transparency of the source PNG better than the command in the answer off [@systho](https://askubuntu.com/a/867568/189996). – wittich Jul 12 '23 at 07:11
  • This should be the accepted answer IMO. The accepted answer results in a blurry image for me. – Astral Axiom Aug 09 '23 at 11:23
4

Use this zsh function:

png2ico () {
    local i="${1}" o="${2:-${1:r}.ico}" s="${png2ico_size:-256}"
    convert -resize x${s} -gravity center -crop ${s}x${s}+0+0 "$i" -flatten -colors 256 -background transparent "$o"
}

Like so:

png2ico input.png
HappyFace
  • 311
  • 2
  • 13