If like me you spend a good deal of your time producing documentation with a healthy handful of images, you might use the code that follows in order to avoid Gimp/Photoshop overuse...the script generates the name, width and height of images spec'ed on the command line. If a number precedes the file spec, it is used to scale the generated information.

#!/perl/bin/perl # # imagexy.pl -- script to return width and height of image(s) use strict; use warnings; use diagnostics; use Image::Size qw(:all); my $scale; if ($ARGV[0] =~ /\d+/) { $scale = shift; } for (map {glob} @ARGV) { my ($x,$y,$id) = imgsize($_); if ($scale and $scale gt '1') { print "$_ width=",$x/$scale,"in, height=",$y/$scale,"in\n"; } else { print "$_ width=$x, height=$y\n"; } }

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re: Image Xs and Ys
by LAI (Hermit) on Jan 29, 2003 at 21:59 UTC

    How about cutting your code down a bit? Also, your regex matches one or more digits in the filename, so an image called mypic1.gif will set $scale to 'mypic1.gif'. What you want is something more like:

    #!/perl/bin/perl # # imagexy.pl -- script to return width and height of image(s) use strict; use warnings; use diagnostics; use Image::Size qw(:all); my $scale = ($ARGV[0] =~ /^\d+$/) ? shift : 1; for (map {glob} @ARGV) { my ($x,$y,$id) = imgsize($_); print "$_ width=",$x/$scale,"in, height=",$y/$scale,"in\n"; }

    LAI
    :eof

      Nice! One minor quibble, if $scale == 1, then result is pixels, not inches...

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

        Whoops, I didn't even notice that.

        Wait... why the hell are you using inches anyway? If you're scaling the image by a factor of 3, let's say, and the image is 120*50 pixels, it'll still be 360*150 pixels. Unless $scale is dpi?

        Which, come to think of it, makes a lot of sense. Oh, I guess that's what you meant in the first place. Never mind me, I'm running low on lifeblood caffeine. But let's say the scale is specified as 1 dot per inch. (big crappy billboards or whatever) Then the measurement should be in inches.

        If this is the case, then the unit of measurement is inches if a dpi is specified, pixels otherwise, and the code is:

        # 'use's and stuff go here my ($scale, $unit) = ($ARGV[0] =~ /^\d+$/) ? (shift,"in") : (1,"px"); for (map {glob} @ARGV) { my ($x,$y,$id) = imgsize($_); print "$_ width=",$x/$scale,"$unit, height=",$y/$scale,"$unit\n"; }

        Update: just a bit of tidying


        LAI
        :eof