Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Changing Image Resolution

by notsoevil (Pilgrim)
on Apr 28, 2001 at 00:35 UTC ( #76264=perlquestion: print w/replies, xml ) Need Help??

notsoevil has asked for the wisdom of the Perl Monks concerning the following question:

I have a script for an 'article manager' web-based interface, where the manager uploads a given image to the server.

Before writing the image to the server, which is to be embedded into the article it is associated with, its width and height are checked:

use Image::Magick; ... # Check and change image height if necessary my ($height,$width); ($height,$width) = $image->GetAttribute('height','width'); if ( $height > 300 ) { my ($newheight,$newwidth); $newheight = 300; $newwidth = ($width*$newheight)/$height; $image->Scale('height'=>$newheight,'width'=>$newwidth); } # Check and change image width if necessary ($height,$width) = $image->GetAttribute('height','width'); if ( $width > 250 ) { my ($newheight,$newwidth); $newwidth = 250; $newheight = ($height*$newwidth)/$width; $image->Scale('width'=>$newwidth,'height'=>$newheight); }

No problem with that code (save it being repetitive in parts, which is necessary as I need to have verbose code for the client, not necessarily 'clean, Perlish' code).

However, I also need to check/change image resolution, which from my understanding should be able to be done like so:

# Adjust Resolution if necessary $image->SetAttribute('units'=>'PixelsPerInch'); my ($xrez,$yrez) = $image->GetAttribute('x-resolution', 'y-resolution'); if ($xrez > 72 || $yrez > 72) { $image->SetAttribute('density'=>'72x72'); }
But alas, the image is written to the server in the following steps with the initial resolution, which could be above 72 and is undesirable.

Embedding print statements before the resolution block of code yields, say for an example image:

x-resolution is 300 y-resolution is 300

After the resolution block, the same print statement yields:

x-resolution is 72 y-resolution is 72

Which would lead one to believe the transformation occurred, although it did not (the image on the server retains the original resolution).

Have any fellow monks run into such problems before?

Jeremiah 49:32 - And their camels shall be a booty. . .

Replies are listed 'Best First'.
Re: Changing Image Resolution
by merlyn (Sage) on Apr 28, 2001 at 04:24 UTC
    An image doesn't have a "resolution". Pixels are pixels. I get into this argument all the time with graphic designers, and even magazines that want me to send "a 300-dpi GIF or JPG". Gaaaah!

    Perhaps what you want is a scaling operation. Try the scale method.

    -- Randal L. Schwartz, Perl hacker

      Okay, I just created two images, with supposed 'resolutions' of 72 and the other of 300. Same dimensions, different resolutions, and same file size. I most certainly should have done so earlier.

      I work for a 'traditional' advertisement agency. Its all about 'CMYK', 'resolution' and 'bleed lines'. The issue of resolution was brought up by my employer, and my understanding of screen 'resolution' versus print resolution were obviously not up to par.

      I have used scale, and it works quite nicely. Thanks for the clarification.

      Jeremiah 49:32 - And their camels shall be a booty. . .

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://76264]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2023-12-08 19:32 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (37 votes). Check out past polls.