z28 has asked for the wisdom of the Perl Monks concerning the following question:
Howdy Monks,
I'm looking to resize images on the fly and the module of choice seems to be Image::Magick. In fact, there seems to be no choice at all as Image::Size can only output size, not change it.
After some perlmonks and google browsing I have come up short. What are my options? Is command line resizing the way to go? May I please have some example code for command line resizing?
As you can tell my knowledge on this subject is very limited. If you have an answer to the question I should have asked, but didn't, your input is more than welcome. :)
Thanks as always,
z28
Re: Resizing images without Image::Magick
by blokhead (Monsignor) on Aug 24, 2002 at 17:58 UTC
|
A quick and dirty way would be the following.. It uses the ImageMagick 'convert' command, although the Perl interface is much cleaner to use from inside Perl:
my $new_size = '640x480';
my $image_file = 'something.jpg';
my $new_image = `convert -size $new_size -resize $new_size $image_file
+ jpg:-`;
print "Content-type: image/jpeg\n\n$new_image";
The 'convert' line of code shows how to resize an image on the fly and print the new image to standard output (probably safest if you're doing CGI). Look through `man convert` for more explanation.
I would highly recommend using the Image::Magick interface, especially in a CGI application. If you're just doing some ad-hoc housekeeping scripts, you may not even need Perl, just some bash scripting. | [reply] [d/l] |
Re: Resizing images without Image::Magick
by silent11 (Vicar) on Aug 24, 2002 at 20:34 UTC
|
This works for me just fine using GD.
#! perl -w
use GD;
my $source = 'meat.jpg';
my $Thumbnail = 'thumb_' . "$source";
my $maxheight = 150;
my $maxwidth = 150;
my $srcimage = GD::Image->newFromJpeg("$source");
my ($srcW,$srcH) = $srcimage->getBounds();
my $wdiff = $srcW - $maxwidth;
my $hdiff = $srcH - $maxheight;
my $newH; my $newW;
if ($wdiff > $hdiff) {
$newW = $maxwidth;
$aspect = ($newW/$srcW);
$newH = int($srcH * $aspect);
} else {
$newH = $maxheight;
$aspect = ($newH/$srcH);
$newW = int($srcW * $aspect);
}
print "converting $srcW:$srcH to $newW:$newH\n";
my $newimage = new GD::Image($newW,$newH);
$newimage->copyResized($srcimage,0,0,0,0,$newW,$newH,$srcW,$srcH);
open(FILE, ">$Thumbnail") || die "Cannot open $Thumbnail : $!\n";
binmode (FILE); #-- ONLY FOR WINDOWS --#
print FILE $newimage->jpeg;
-Silent11 | [reply] [d/l] |
|
GD is a nice, easy-to-use tool, without the bulk of Image::Magick. But the catch, of course, is that it only works with paletted images.
| [reply] |
|
sub resize {
use GD;
my($data, @size) = @_;
my $image = GD::Image->newFromJpegData($data, 1);
my @isize = $image->getBounds();
my $new_image = GD::Image->new(@size, 1);
$new_image->copyResampled($image,0,0,0,0,@size,@isize);
return $new_image->jpeg(75);
}
http://search.cpan.org/author/LDS/GD-2.01/GD.pm if you need help. | [reply] [d/l] |
|
|
Re: Resizing images without Image::Magick
by Juerd (Abbot) on Aug 24, 2002 at 21:16 UTC
|
| [reply] |
•Re: Resizing images without Image::Magick
by merlyn (Sage) on Aug 24, 2002 at 18:31 UTC
|
I'm looking to resize images on the fly and the module of choice seems to be Image::Magick.
Yes, it is. So what's your question? You've not yet said why Image::Magick is not your solution. This would be necessary before any answer will be very useful.
-- Randal L. Schwartz, Perl hacker
| [reply] |
Re: Resizing images without Image::Magick
by fglock (Vicar) on Aug 25, 2002 at 01:54 UTC
|
PDL module is even more powerful than
Image::Magick. It is my module of choice
for scientific applications.
| [reply] |
|
I am interested in PDL, but it seems so overwhelming. Could you give an example of how you would do this task using the PDL module(s)?
Impossible Robot
| [reply] |
|
/me trying to ssh my server to find an example... Where is putty.exe? No way - server is down and it's saturday. Can't see http://pdl.perl.org either. Weird.
PDL will read() and write() images just like
ImageMagick, but it will also allow you to access
the image bits as data (an image is a perl object).
And PDL comes with lots of image
processing functions.
I'm sorry, I can't give you any examples
right now :(
| [reply] |
Re: Resizing images without Image::Magick
by Anonymous Monk on Aug 25, 2002 at 01:11 UTC
|
>In fact, there seems to be no choice at all as Image::Size can only output size, not change it.
It seems an important question is whether you want to mess with the size(pixels) or the weight(kilobytes). If all you're trying to do is change the *html display* size , I've used
Image::Size
succesfully for this.
###############
use Image::Size;
$pixel_path = "/path/$directory/$file_name";
($width, $height) = imgsize("$pixel_path");
$new_width = $width/4;
$new_height = $height/4;
print qq~
<img src="/path/to/image/file_name.jpg" width="$new_width" height="$new_height">
~;
##############
The above (which would display the image at 1/4 size) has been working fine for the applications I use it in. Maybe it'd work in yours?
I'm new to the monastery - not sure what the protcol is here about signatures, etc ... hope this is of some help
Carl Hagstrom
hagstrom@epix.net
| [reply] |
|
What you've created there is called a dumbnail (as opposed to a thumbnail) and is generally considered bad practice. The web client still has to download the big file, so its not the prefered way of displaying a smaller image.
-Blake
| [reply] |
Re: Resizing images without Image::Magick
by belg4mit (Prior) on Aug 26, 2002 at 23:25 UTC
|
| [reply] |
|
|