Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ImageMagick getimagesize in perl???

Status
Not open for further replies.

LocoPollo

Programmer
Jun 6, 2005
48
DK
How do I get the original size of the image in perl?? I have tired this and alot of other things:

#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
use Image::Magick;

my $workimage = Image::Magick->new;

$workimage->Read("./test.jpg");
my @imginfo = getimagesize($workimage);
print"$imginfo[0] x $imginfo[1]";

undef($workimage);

---------

Thanks again!
Peter
 
Hi

I can only help with this (must use BACKTICKS not single speech marks):-

Code:
$result = `identify test.jpg`;
print $result;

... as i don't have PerlMagick installed

You can split up the result as it always consistent

Cheers
Duncan


Kind Regards
Duncan
 
Cool! :) And thanks alot, from ZERO, you got me 95% happy. A oneliner would have brought me to 110% ;-)
I get a string like this:

test.jpg JPEG 242x337+0+0 DirectClass 8-bit 7kb 0.000u 0:01

I'm working on it, and it gets a bit clumbsy, but it DOES works!.. First I have to split where there are spaces, then replace the +0+0, then split again where there is a X.

So if someone knows a spiffy way to get the height and weight in just one line, it would be even greater. Cant find much documentation on it!?

Once again thanks Duncan.

BTW: what does the backtips do?? I never read about those before? Do they execute commandline things?
 
thanks LocoPollo

just off to bed (UK)

this does the trick:-

Code:
[b]#!/usr/bin/perl[/b]

$_ = 'test.jpg JPEG 242x337+0+0 DirectClass 8-bit 7kb 0.000u 0:01';

m/^[^.]+\.[^ ]+ [A-Z]+ ([^x]+)x([^+]+).*$/;

print $1; # 1st dimension
print "\n";
print $2; # 2nd dimension


Kind Regards
Duncan
 
You tricked some keywords here, and I found this solution...

Code:
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
use Image::Magick;

my $image = Image::Magick->new;
$image->Read("./test.jpg");
$width = $image->Get('columns');
$height = $image->Get('rows');
print "$width x $height";
undef($image);

Incase someone else can use it, I found it here:

Wauw! your oneliner is hardcore!?
I have 100 questions to that one, but here are two if you dont mind, and I know it's basic, but I'm still using "my $something", for everything.

$_ when does that one contain any data, and for how long? until first { or }

..the same question with $1 and $2??

Thanks alot once again. :))
 
there is also:

my $dims = `imgsize test.jpg`;
print $dims;
 
Hi LocoPollo

Thank you for your kind comments

This is a breakdown of the regex - I hope it is of use!?

my $something = 'test.jpg JPEG 242x337+0+0 DirectClass 8-bit 7kb 0.000u 0:01';

$something =~ m/^[^.]+\.[^ ]+ [A-Z]+ ([^x]+)x([^+]+).*$/;

( note - i have used hashes to denote spaces for visibility )

Code:
m/                --- start of match ---
^                 FORCES REGEX TO START MATCHING AT BEGINNING OF LINE
[^.]+             1 or more chars that aren't a dot
\.                a dot
[^#]+             1 or more chars that aren't a space 
#                 a space
[A-Z]+            1 or more uppercase chars
#                 a space
([^x]+)           1 or more chars that aren't an 'x' - capture as $1
x                 an 'x'
([^+]+)           1 or more chars that aren't an '+' - capture as $2
.*                zero or more characters (be careful as this is greedy)
$                 FORCES REGEX TO MATCH TO END OF LINE
/                 --- end of match ---


Kind Regards
Duncan
 
Hi LocoPollo

Regarding the $_

For example you might write a loop as follows:-

Code:
[b]#!/usr/bin/perl[/b]

@names = ( 'name1',
           'name1',
           'name3' );

foreach $name (@names) {
  print "$name\n";
}

but you could omit the $name as follows:-

Code:
[b]#!/usr/bin/perl[/b]

@names = ( 'name1',
           'name1',
           'name3' );

foreach (@names) {
  print "[b]$_[/b]\n";
}

Be careful with this - it can be quite dangerous

Basically it is a predifined variable - if you have not been EXPLICIT (i.e. specifying the variable to hold the individual values of the array in this instance) then it IMPLICITLY Perl knows. A good example would be if you wre in a room with one other person and they said 'Do you have the time please' - then you would assume they meant you! The example explains it better!


Kind Regards
Duncan
 
Sorry, didn't see the additions until now!

Thanks alot, it was all very useful, now I know more about regex, it has always been a big mysterious!? Thanks for carving it out for me. :)

And that temp-thingy: $_ ... I dont know, I might start using it, but it sounds a like a dangerous little thing.

You didn't gave any examples of when it can go wrong, but I guess thats it will if I use one of them inside another one. (e.g. for/foreach) I will only use it in small foreach's. :)

$1 and $2 thats only for matches as far as I can see elsewhere on the net, or can I use it in other case?

Anyway, I will start playing with all of it! hehe

Thanks again everyone, this made my day! :eek:)
 
Hi LocoPollo

Thanks again for your kind comments - and i'm really glad you are enjoying learning!

You are quite right about the $_ variable - with nested loops this implied method won't work. And you are also correct about the $1 $2 etc. - these are the captures within the regular expression. These are populated on a match if the regex contains braces - i.e. just before and after the 'x' - giving you a separate capture for both the horizontal and vertical dimensions of the image

Sorry i did not explain fully in an earlier post

Carry on having fun!


Kind Regards
Duncan
 
Technically, you *can* use $1, $2, $3 etc and $a, $b anywhere you want. You probably *shouldn't* though, as Perl will take them over when it needs them as special variables.
 
Would it not seem a shame to have to install a module just to find this out? Please tell me if this is so as I have yet to install a single module after using Perl for many years. Maybe i am being very naiive. Thanks.


Kind Regards
Duncan
 
considering `identify test.jpg` and `imgsize test.jpg' are OS specific (unix), using Image::Size should be more portable. Part of the power of perl is it's portability, so many will say it's best to use perl and perl modules as often as possible.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top