Skip to content

Instantly share code, notes, and snippets.

@pfig
Created February 12, 2012 12:01
Embed
What would you like to do?
Make a multi-resolution favicon.ico from a source image, using ImageMagick
#!/bin/bash
# from
# http://bergamini.org/computers/creating-favicon.ico-icon-files-with-imagemagick-convert.html
convert source-WxW.png -resize 256x256 -transparent white favicon-256.png
convert favicon-256.png -resize 16x16 favicon-16.png
convert favicon-256.png -resize 32x32 favicon-32.png
convert favicon-256.png -resize 64x64 favicon-64.png
convert favicon-256.png -resize 128x128 favicon-128.png
convert favicon-16.png favicon-32.png favicon-64.png favicon-128.png favicon-256.png -colors 256 favicon.ico
@mikeraz
Copy link

mikeraz commented May 27, 2013

For some reason, I needed a fully shaved yak. As a result:

#!/bin/bash

# make a multi-layer favicon.ico via ImageMagick
# todo 
#  maybe convert to Perl with Image::Magick ?
#  check if image has 1:1 aspect ratio, warn if not
#  ensure image file is sane size

FILE=$1
EXT=`echo $1 | awk -F . '{print $NF}'`
BASE=`basename $FILE .$EXT`
shopt -s nocasematch

if [ -f $FILE ];
then
    case "$EXT" in 
        jpg | gif)
            echo Hint: non-png files are suboptimal
            echo Creating  $BASE.ico 
        ;;
        png )
            echo Good, a png file, creating $BASE.ico
        ;;
        *)
            echo Must use a jpg, gif, or \(preferred\) png image
            exit
        ;;
    esac
else
    echo no file $FILE
    exit
fi 

SIZES=""
for SIZE in 16 32 64 128
do 
    convert -resize x$SIZE $FILE -background transparent $BASE-${SIZE}.png
    if [ $SIZES ];
    then
        SIZES=$SIZES,$SIZE
    else
        SIZES=$SIZE
    fi
done
convert $BASE-{$SIZES}.png -background transparent -colors 256 $BASE.ico
rm $BASE-{$SIZES}.png 

@BersBaD
Copy link

BersBaD commented Oct 27, 2013

keep it short and simple

RTFM

http://www.imagemagick.org/Usage/thumbnails/#favicon

convert favicon.png  -bordercolor white -border 0 \
      \( -clone 0 -resize 16x16 \) \
      \( -clone 0 -resize 32x32 \) \
      \( -clone 0 -resize 48x48 \) \
      \( -clone 0 -resize 64x64 \) \
      -delete 0 -alpha off -colors 256 favicon.ico

@kris-luminar
Copy link

@BersBaD 's solution worked perfectly for me. Thanks!

@nickpearson
Copy link

This is simpler in newer versions of ImageMagick (seemingly 6.8.8-3 and later), and background transparency is retained with this:

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

Source: dlemstra's comment on http://www.imagemagick.org/discourse-server/viewtopic.php?t=26252

@niittymaa
Copy link

I found mogrify quite useful in converting icons to multilayered ICO format from SVG images. This command batch converts all SVG images from selected folder and converts those to multilayered ICO format.

mogrify -path ../PathToFolder/ -format ico -density 600 -define icon:auto-resize=128,64,48,32,16 *.svg

@eberkund
Copy link

Does this work with SVGs?

@graingert
Copy link

@eberkund yes

@jcklpe
Copy link

jcklpe commented Aug 20, 2018

@niittymaa Is it possible to use this with a higher resolution like 512?

@sec0ndhand
Copy link

If you have docker installed this works and writes the ICOs to the same folder where your SVGs are:

docker run -v /Path/to/your/local/SVGs:/images -it umnelevator/imagemagick mogrify -path /images -format ico -density 600 -define icon:auto-resize=256,128,64,48,40,32,24,16 /images/*.svg

just replace /Path/to/your/local/SVGs with your folder of SVGs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment