today's topic is quite easy: retrieving image size (in perl, with the help of cpan of course). indeed, when one wants to resize an image, it's often interesting to know its current size.
the easiest way to retrieve those information is of course to use image::size:
easy, wasn't it?
however, as mentioned previously, retrieving those information is often just the prelude before doing some transformation to the image itself. and image::size, while doing it perfectly, does only one thing - it cannot be used for any other image manipulation. so if you intend to use another module after, you can as well use this other module to retrieve this information: that'll be faster (image read only once) and use less memory (only one module loaded). here's how to get those information with image::magick:
it wasn't that difficult, and opens the whole image magick world to your program!
Showing posts with label image. Show all posts
Showing posts with label image. Show all posts
2010-06-08
2009-06-22
where are builtin tk icons? (aka tk::toolbar fun)
for everything gui related, my favorite toolkit is tk. it's quite powerful, easy to learn and to use, and feels quite perlish [0]. in a word, it doesn't get in the way.
yeah, of course, current version is not the most up to date [1][2] - but it gets the job done, with the help of some nice tk additional modules available on cpan.
one of those modules is tk::toolbar, from ala qumsieh. and it comes with a set of bundled icons that are loaded. they may not be the prettiest icons out there, but they are available and allow you to do something like [3]:
knowing this, when i needed to add an icon in my new project (yes, yes, i need to present it here), i loaded tk::toolbar in my code, and re-used one of the bundled images.
well, that was my goal anyway - the image never appeared... i tried to tweak my code in every direction, but did not manage to make this icon appear.
having rebuild perl-tk package yesterday to fix a bug in mandriva's package, i thought it might be related... so i tried to run a perl/tk gui provided in one of my other modules using tk::toolbar's icons, but the icons were correctly displayed. so long for this idea...
after having fought 5 minutes, i finally managed to understand what was going on: in this new project, i currently do not have created the toolbar. and since the icons are loaded in the classinit() method, called during the creation of the first widget of this particular class, the icons were not loaded...
so i created a toolbar [4], and miracle! the icon i wanted magically appeared.
tricky one...
[0] contrary to wxwidgets for example - at least imnsho
[1] i'm using plain tk 804.028 and not one of those tk ersatz tcl::tk [5] or tkx [6]
[2] slaven told me he would like to update it when he'll have some tuits...
[3] tk allows you to name the images you load, to do for example:
[4] i'll populate the toolbar later on
[5] which is far from complete, and does not seem to be maintained anymore
[6] which doesn't compile easily out of the box, and feels really clumsy after tk goodness. oh well, i guess beauty is in the eye of the beholder...
yeah, of course, current version is not the most up to date [1][2] - but it gets the job done, with the help of some nice tk additional modules available on cpan.
one of those modules is tk::toolbar, from ala qumsieh. and it comes with a set of bundled icons that are loaded. they may not be the prettiest icons out there, but they are available and allow you to do something like [3]:
instead of having to create & load the images yourself.$mw->Label( -image => 'fileopen16' )->pack;
knowing this, when i needed to add an icon in my new project (yes, yes, i need to present it here), i loaded tk::toolbar in my code, and re-used one of the bundled images.
well, that was my goal anyway - the image never appeared... i tried to tweak my code in every direction, but did not manage to make this icon appear.
having rebuild perl-tk package yesterday to fix a bug in mandriva's package, i thought it might be related... so i tried to run a perl/tk gui provided in one of my other modules using tk::toolbar's icons, but the icons were correctly displayed. so long for this idea...
after having fought 5 minutes, i finally managed to understand what was going on: in this new project, i currently do not have created the toolbar. and since the icons are loaded in the classinit() method, called during the creation of the first widget of this particular class, the icons were not loaded...
so i created a toolbar [4], and miracle! the icon i wanted magically appeared.
tricky one...
[0] contrary to wxwidgets for example - at least imnsho
[1] i'm using plain tk 804.028 and not one of those tk ersatz tcl::tk [5] or tkx [6]
[2] slaven told me he would like to update it when he'll have some tuits...
[3] tk allows you to name the images you load, to do for example:
$mw->Photo( "foo", -file=>"path/to/some/image.jpg" );
[... later on ...]
$mw->Label( -image => "foo" )->pack;
[4] i'll populate the toolbar later on
[5] which is far from complete, and does not seem to be maintained anymore
[6] which doesn't compile easily out of the box, and feels really clumsy after tk goodness. oh well, i guess beauty is in the eye of the beholder...
2009-05-29
image resizing in perl
perl is really versatile, and can do a lot of things. moreover, you can do it in a lot of different ways. the difficult part being sometimes to choose the way you want to do it...
so i need to resize an image within perl. this is for games::risk, a perl implementation of the famous board game (btw, try it and tell me if you like it!). i want the background image to fit the window size on resize events. of course, i want it to be fast - but i also want something maintainable.
looking on cpan, i found 3 modules: gd, image::magick and image::imlib2. using gd to resize a picture is a nightmare, therefore i ditched it for image::resize which is a convenient wrapper around gd.
here are the needed incantations to resize:
however, image::imlib2 has a drawback: the only way to get back the new image is to save it to a file, where one can get the new image as a scalar directly:
therefore, one must also take this into account in the benchmarking! so here's the latest version of the bench:
and i was quite astonished to see that image::imlib2 is still 2 or 3 times faster that image::magick or image::resize!
so, imlib2 may not the best api around, but sure it's fast...
so i need to resize an image within perl. this is for games::risk, a perl implementation of the famous board game (btw, try it and tell me if you like it!). i want the background image to fit the window size on resize events. of course, i want it to be fast - but i also want something maintainable.
looking on cpan, i found 3 modules: gd, image::magick and image::imlib2. using gd to resize a picture is a nightmare, therefore i ditched it for image::resize which is a convenient wrapper around gd.
here are the needed incantations to resize:
- with imlib2
- with image::magick - note that the operation is done inplace, so one needs to clone the image first to compare the same things.
- with image::resize
my $old = Image::Imlib2->load($src);
my $new = $old->create_scaled_image($w, $h);
my $old = Image::Magick->new;
$old->Read($src);
my $new = $old->Clone;
$new->Scale(width=>$w, height=>$h);
my $old = Image::Resize->new($src);
my $new = $old->resize($w, $h);
however, image::imlib2 has a drawback: the only way to get back the new image is to save it to a file, where one can get the new image as a scalar directly:
- image::resize with $img->jpeg
- image::magick with $img->ImageToBlob
therefore, one must also take this into account in the benchmarking! so here's the latest version of the bench:
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use Benchmark qw{ :all };
use Image::Size;
use Image::Resize;
use Image::Imlib2;
use Image::Magick;
my $src = "src.jpg";
my $dst = "dst.jpg";
my ($w, $h) = imgsize($src);
my @sizes = (
[10,10], [100,100], [1000,1000],
[640,400], [840,600], [1024,768],
[$w,$h], [$w/2,$h/2], [$w/4,$h/4],
[$h,$w], [$h/2,$w/2], [$h/4,$w/4],
);
my $imlib2 = Image::Imlib2->load($src);
my $resize = Image::Resize->new($src);
my $magick = Image::Magick->new; $magick->Read($src);
foreach my $s ( @sizes ) {
my ($width, $height) = @$s;
say "-> ${width}x${height}";
local $/;
cmpthese( -3, {
imlib2 => sub {
my $img = $imlib2->create_scaled_image($width, $height);
$img->save($dst);
open my $fh, '<', $dst;
return <$fh>;
},
magick => sub {
my $img = $magick->Clone;
$img->Scale(width=>$width,height=>$height);
return $img->ImageToBlob;
},
resize => sub {
my $img = $resize->resize($width,$height);
return $img->jpeg; # $img->png
},
} );
say '';
}
and i was quite astonished to see that image::imlib2 is still 2 or 3 times faster that image::magick or image::resize!
so, imlib2 may not the best api around, but sure it's fast...
Subscribe to:
Posts (Atom)