Category Archives: Perl

Fetching Image details in Perl

Image::Size is fine, if size is the only thing, which matters. Sometimes, however, it isn’t enough, and when that is the case Image::Info (again fetched from CPAN) is your friend. Point it to a file (through various methods), and it will return a hash with all the information available about the image you pointed at. Most popular formats are supported.

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/perl -w
use strict;
use Image::Info;
 
# Just fetch the size
my $imgInfo = Image::Info::image_info("test.jpg");
 
# Print out all info fetched from the image
for (keys %$imgInfo) { print " $_ -> $imgInfo->{$_}n"; }
 
exit();

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();

Rotating an Image with Perl

Turning images is quite simple. In the example below an image is turned 90 degrees clockwise, wirtten to a file, turned another 90 degress and written to a file again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/perl -w
use strict;
use Image::Magick;
 
my $image = Image::Magick->new(magick=>'JPEG');
my $x = $image->Read('test.jpg');
 
$x = $image->Rotate(degrees=>90); # 90 degress clockwise
$x = $image->Write('test.90.jpg');
 
$x = $image->Rotate(degrees=>90); # Another 90 degress clockwise
$x = $image->Write('test.180.jpg');
 
exit();

Making thumbnails with Perl

With the help of ImageMagick you can automagically use Perl to create thumbnails. The example below is quite rude and makes a 50 by 50 thumbnail (no matter which size and shape the master had). Before using it in a real world scenario, check the aspect ratio, the size of the original image and what ever may be applicable.

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(magick=>'JPEG');
my $x = $image->Read('test.jpg');
 
$x = $image->Scale(width=>'50', height=> '50');
 
# The following should also work fine...
# $x = $image->Scale(geometry=> '50x50');
 
$x = $image->Write('test.50x50.jpg');
 
exit();

IP address conversion with Perl

With Perl you can do many interesting transformations of IP-numbers. Below is two small examples allowing conversions from “IP quad” (xxx.xxx.xxx.xxx)
format to a single decimal and back. The decimal format may be more convenient and efficient to store in a database.

  sub ip2dec ($) {
    return unpack N => pack CCCC => split /\./ => shift;
  }

  sub dec2ip ($) {
    return join '.' => map { ($_[0] >> 8*(3-$_)) % 256 } 0 .. 3;
  }

In CPAN you can find many modules aimed at using and manipulating IP-addressees.

Some include Net::IP and IP::Country.

Bulk resizing images with Perl

Suppose you’ve just filled you digital camera with an endless stream of photos. You want to place them online at your website, but placing 5+ megapixel files online, well…probably a bad idea. Let’s resize them to a propper size – and why not use Perl and ImageMagick for the job.  Not a problem, here’s a complete example on how to resize all images in a directory . Make sure you have ImageMagick installed.

Converting between image formats with Perl

Changing files from one format to another is quite easy with a little bit of ImageMagick . 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.

[php] no_tags
#!/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();[/php]

Image sizes in Perl

If you need for figure out the size of an image, fetch Image::Size from CPAN , it’s just what you need. The module recognizes the most common image-formats such as JPG, GIF, PNG, PSD, TIFF and plenty more. The interface is simple and will get you what you need with no trouble at all.

[php] no_tags
#!/usr/bin/perl -w
use strict;
use Image::Size;

# Just fetch the size
my ($size_x, $size_y) = Image::Size::imgsize(‘test.jpg’);
print “Image is: $size_y x $size_x (height x width)\n”;

# HTML-friendly format
my $size = Image::Size::html_imgsize(‘test.jpg’);
print “Here is my image \n”;

exit();
[/php]

Search in a LDAP directory

This example connects to a LDAP server and makes a search for a name. The name was choosen by random (among those who returned an answer from the queried LDAP). The LDAP used in this example includes a binary certificate. To prevent this from trashing you terminal, it is not printed to the screen (binary field filtered in the attribute loop).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl -w
use strict;
 
use Net::LDAP;
my $ldap = Net::LDAP->new('directory.certifikat.dk') or die "$@";
$ldap->bind ;    # an anonymous bind
 
my $mesg = $ldap->search (  # perform a search
                       base   => "c=DK",
                       filter => "(&(cn=Henrik Jensen))"
 
                      );
 
$mesg->code && die $mesg->error;
 
print STDERR "Found " . $mesg->count . "n";
foreach my $entry ($mesg->all_entries) {
  my  @values = $entry->attributes();
  foreach my $key (@values) {
    print "$key => \"" . $entry->get_value($key) ."\"n" unless ($key =~ /binary/);
  }
}
 
exit();

Mark all messages as read in an imap folder

The follow script marks all files in a folder as read. You need to pass hostname, username and password as commandline parameters to the script and the script is hardwired to mark all files in a folder call “INBOX.spam” (Cyrus IMAP folder naming convention).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl -w
use strict;
 
use Mail::IMAPClient::BodyStructure;
use Mail::IMAPClient;
 
my ($serv, $usr, $pwd) = (@ARGV); # server, username and password as comandline parameters...
 
my $imap = Mail::IMAPClient->new(Server=>$serv,User=>$usr,Password=>$pwd);
my @folders = $imap->folders;
 
foreach my $f (@folders) {
  print 	"$f is a folder with ", $imap->message_count($f), " messages.n";
}
 
exit();