Converting between image formats in Perl

Changing files from one format to another is quite easy with a little bit of Magick. In the example below a JPG image (test.jpg) is converted into a GIF-image (test.gif). To output in a different (ImageMagick supported) format, just change the “image->Set” line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl -w
use strict;
use Image::Magick;
 
my $image = Image::Magick->new();
 
# To explicitly set image format use this instead:
# my $image = Image::Magick->new(magick=>'JPEG');
 
my $x = $image->Read('test.jpg');
 
$x = $image->Set(magick => 'GIF');
$x = $image->Write('test.gif');
 
exit();