Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

PerlMagick: How to Calculate an image size in milimiters?

by monsieur_champs (Curate)
on Jun 03, 2003 at 21:18 UTC ( #262796=perlquestion: print w/replies, xml ) Need Help??

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


I'm building a system to crop, resample and scale TIFF images from a Fax server, so it fit into a set of A4 (210x297mm) pages.

However I'm facing a strange problem: I don't know for sure how to calculate the size of a Image in millimeters. My calculations simply don't correspond to what I'm expecting, except when the resolution is 72dpi.

I'm using the PerlMagick ImageMagick Interface to this task.

To calculate the image size, I'm using this:

# A4 page height in inches (approximately) use constant PAGE_HEIGHT => 11; # A4 page width in inches (approximately) use constant PAGE_WIDTH => 8; my $image= new Image::Magick(); $image->Read( 'filename.tif' ); $vertical_size = $image->Get( 'y-resolution' ) * PAGE_HEIGHT; $horizontal_size = $image->Get( 'x-resolution' ) * PAGE_WIDTH;

Can anybody help me with this task?

May the gods bless you.

Luis Campos de Carvalho
Just Another Perl Monk

Replies are listed 'Best First'.
Re: PerlMagick: How to Calculate an image size in milimiters?
by Arguile (Hermit) on Jun 03, 2003 at 21:53 UTC

    The $image->Get( 'y-resolution' ) returns the number of pixels. Think of them as the ‘dots’ in <acronym title="Dots Per Inch">DPI</acronym>.

    To determine the printable size from this without rescaling, you’d divide by the DPI to get the size in inches. I’ll leave the imperial -> metric conversions to you.

    If you plan to resample an image ‘to-fit’ what you’d want is to determine the factor by which to resize. To do this you’d take the maximum paper size times the dpi, then divide by the image size.

    use constant PAGE_WIDTH => 8; # 8.5" page with 0.5" of equal margins. use constant DPI => 72; # Fax resolution. # ... $scale_factor = ( PAGE_WIDTH * DPI ) / $image->Get( 'y-resolution'); # ...

    The above however doesn't take into acount which dimension we need to fit to scale. If you want to maintain the aspect ratio you’d need to figure out which dimension to scale to, easiest would be to figure out both and take the min. factor.

    You could also choose to switch orientation so that the highest pixel count always went along the longest page dimension.


    8.5" x 11" is not A4 , it’s letter. I just used those dimensions for convenience. As for faxes, I think they can be 150DPI as well, but it would pay to check.

      Faxes use the CCITT Group 3 protocol. They support 2 resolutions: 203 by 98 dpi and 203 by 196 dpi. So, the code you listed will need to be modified slightly to accommodate different resolutions in the x and y axis. Here is a detailed description of the fax protocol.

      >As for faxes, I think they can be 150DPI as well, >but it would pay to check.

      I'd really like to know the answer to that as well -- I had a rather bizarre incident the other day in which a colleague wanted to fax the drawn outline of his foot to a specialist bootmaker in Poland.

      As just a few mils either way could make all the difference to an item of footwear, I advised him not to -- who knows what actual size it's going to come out the other end?

      Presumably faxes have some kind of handshaking process where they try and establish what DPI they can both safely use? And what's the margin of error allowed in the standard?

      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2022-06-25 11:11 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (81 votes). Check out past polls.