Category Archives: imagemagick

create 1px alpha transparent png

We needed a background with opacity x% (where x could vary between 30 and 75). We decided to use a “alpha-transparent” PNG24.

How to generate such a PNG using ImageMagick?
Extremely simple – specify a “8-digit” color code using the syntax:

convert -size 1x1 xc:#123456DD test.png

The last 2 digits in the color code (DD) is the alpha channel(opacity/transparency value).

I also wrote a bash-script that generates 255 images for each value of the alpha-channel for a given color:

#!/bin/bash
#
# generate a directory containing 255 semi-transparent png24-files
# with transparencies from 0 to 100% (0-255) for a given color
# author: Andreas Philippi - Cubus Arts 2010                                                                                                                         
#
                                                                                                                                                                       
#convert(Imagemagick must be installed)
type -P convert &>/dev/null || {                                                                                                                                        
    echo "Imagemagick not found on your system." >&2; 
    echo "Debian/Ubuntu: sudo apt-get install imagemagick" >&2; 
    echo "Centos/Red Hat: yum install ImageMagick" >&2; 
    echo "FreeBSD: pkg_add -r ImageMagick" >&2; 
    exit 1; 
    }

#this script gets 1 parameter: the color code
if [ $# == 0 ]; then
   echo 'Usage: transparent_png_creator.sh ';
   echo 'Example: transparent_png_creator.sh 4F6681';
fi

#check the color code
if ! [[ "$1" =~ ^[0-9,A-F,a-f]+$ ]] ; then
    echo "Invalid color code!! Valid range: 000000 - FFFFFF" >&2; 
    exit 1; 
fi
   

#create the directory 
`mkdir $1`

for i in {0..255}
do
   hex_i=$(printf "%02x" $i)
   convert -size 1x1 xc:#$1$hex_i $1/$1_$hex_i.png
done

echo "Successfully created 255 images in directory $1"