Create a CSS sprite from smaller images using Perl and Image::Magick

To create a montage of images suitable for use as a CSS sprite from a collection of smaller images,
# Pack all the bitmaps into one larger file.

use Image::Magick;
my @images = <img/*.png>;
my $im = new Image::Magick;
for my $image (@images) {
    $im->Read ($image);
}
# Make a row of the images
my $tile = scalar (@images) . "x1";
# Add eight pixels of space around each image.
my $output = $im->Montage (tile => $tile, geometry=>'+8+8');
$output->Write ("sprite.png");
This reads all the PNG files from a subdirectory img and writes them into one larger file called sprite.png.

It assumes that all the images are the same size.


Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer