http://www.perlmonks.org?node_id=28183

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (files)

Update by Corion for QAndAEditors

Anonymous Monk had to add the following to his question :

I don't know if my way to use Image::Size is wrong. What's wrong? Here is my code:

#!/usr/bin/perl use CGI qw(:standard); use Image::Size; my @temp=(); my $tmp=""; @temp = imgsize("boy1.jpg"); print header(); foreach $tmp(@temp) print "$tmp";

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I get image height and width without using Image::Size?
by lhoward (Vicar) on Aug 17, 2000 at 01:27 UTC
    There are several other modules other than Image::Size that you can use to get the height and width of an image. You could try Image::Info or Image::Magick.

    But if you want to avoid using modules entirely you're going to have to learn how to read image headers. The headers are different for different types of images (.jpg, .png, etc...). But in general most image files have the height and width stored (generally as 16 or 32 bit integers) somewhere within the first 100 bytes or so of the image.

Re: How can I get image height and width without using Image::Size?
by fundflow (Chaplain) on Aug 17, 2000 at 01:21 UTC
    I sometimes use the `file` command - it's output is easy to parse and can deal efficiently with many images.

    The problem is that it is not very standard and so my linux version gives more details than the solaris one

Re: How can I get image height and width without using Image::Size?
by Anonymous Monk on Aug 20, 2000 at 13:54 UTC
    I have put the curly braces around the codes.. but it doesn't work. Here is the error message: Can't locate Image/Size.pm in @INC What can I do??

      Then the module Image::Size is not installed on the relevant machine.

      You can find modules at CPAN. Installation is easy if you have superuser access on the relevant machine. If you are using ActiveState Perl, you might want to check their site with the package manager.

      Perl has an installation program for modules, perl -MCPAN -e 'shell', which tries to make download, compilation (if necessary), testing and installation of modules as painless as possible, but the CPAN module supplied with ActiveState build 612 has some issues; look at the ActiveState site to remedy this.

      Specific for the Image::Size installation, you don't need to either compile it or use the CPAN module. The INSTALL file supplied in the .tar.gz explains how you can manually install Image::Size on any machine.

Re: How can I get image height and width without using Image::Size?
by Anonymous Monk on Aug 22, 2000 at 23:21 UTC
    I have put the curly braces around the codes.. but it doesn't work. Here is the error message: Can't locate Image/Size.pm in @INC What can I do??
      Is Image::Size installed? Is it installed somewhere other than the directories in @INC?

      perl -e 'print $_ . "\n" for @INC'

      That will show you what is in @INC. If it is installed elsewhere, use this in your script:

      use lib qw(/path/to/it);

      Remember to use the path *up to* the Image directory. So, if it is installed in /home/you/perl/Image/Size.pm, use /home/you/perl
      If it isn't installed, then:

      perl -MCPAN -e 'install Image::Size'

      Cheers,
      KM

Re: How can I get image height and width without using Image::Size?
by rapidwiz (Initiate) on Jun 26, 2007 at 03:05 UTC

    I like Image::Size. (However, I was unable to get it working on an older RedHat system due to missing modules.) You should use it if you can because it is much more efficient, as it doesn't spawn a process. Here's how:

    use Image::Size; # get the image size, and print it out my( $width, $height ) = imgsize( $imagefile_pathname );

    But if you want to do it purely using a system command and ImageMagick's identify, here's how:

    # (assume identify is in the path) chomp( my $ident = `identify -format "%w %h" "$imagefile_pathname"` ); my( $width, $height ) = split ' ', $ident;

      Why all those steps?

      chomp (my $id = `identify $image_file`); my ($x, $y) = ($id =~ m/\s([0-9]+)x([0-9]+)\s/);

      And yes, I also use Image::Size when available.


      Enjoy, Have FUN! H.Merijn

        This of course does not work for files that are called My pic of a 4x4 car.jpg :)

        But identify has options!

        my ($x, $y) = (`identify -format "%w,%h" -quiet $image_file` =~ m/(\d+)/g);

        In whatever prefered context or style.


        Enjoy, Have FUN! H.Merijn
Re: How can I get image height and width without using Image::Size?
by Corion (Patriarch) on Aug 17, 2000 at 11:45 UTC

    The problems stated after the question could be solved by looking at what Perl prints in response to the above code :

    syntax error at tmp.pl line 14, near ") print" Execution of tmp.pl aborted due to compilation errors.

    Which means that Perl does want curly braces around the code to be processed in a foreach loop.