Script CLI instead of PerlMagick

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
victor_charlie

Script CLI instead of PerlMagick

Post by victor_charlie »

With ImageMagick convert or mogrify is the option -auto-orient. If your camera has its AutoOrient option turned on, the EXIF file writes the camera's orientation to the shot. ImageMagick manually rotates the image so you have no surprises when posting that image to the web. Otherwise, programs like Photoshop silently rotate a JPEG for >>viewing<<, lets you make changes, SaveAs and when posted to the web, the image is a vertical image laying on its side. -auto-orient is an expensive operation when converting a folder full of JPEG. After much trial-and-error (I had to learn perl), I made this simple script. It uses the Command-Line-Interface in Linux instead of PerlMagick. I used CLI instead of the PerlMagick interface for the following reasons...

1. I need the value of the EXIF:['Orientation']. the code then branches with ... if > 1, do -auto-orient
2. I can get the image specs I want with a compound command in ImageMagick. Here it is:

Code: Select all

$command = "convert -auto-orient -resample 96 -resize \"x640\" -strip $_ converted/$fname.png";
My goal is best image for SVGA on MS platforms for web and PowerPoint usage. I can gang the ImageMagick options with a CLI. I cannot gang the options with PerlMagick (or can I ? ? ? ?).
3. I needed to capture the value of EXIF:['Orientation'], and as a numeric. I used ExifTool, which is another tool, separate from ImageMack. Here's the perl script...

Code: Select all

#!/usr/bin/perl -w

use Image::ExifTool qw(:Public);
### [[ http://www.sno.phy.queensu.ca/~phil/exiftool/ExifTool.html ]] ###

while( glob('*.JPG') ) {

	my $exifTool = new Image::ExifTool;
	my $meta = $exifTool->ImageInfo( $_ ); # required, constructs the ObjectFile:EXIF, or use perl bless()
	my $tag = "Orientation";
	my $v = $exifTool->GetValue($tag, 'ValueConv'); # reads the exif values, fetches a field, ValueConv as numeric

	my $fname = substr( $_, 0, rindex($_, '.') ); # strips the .JPG off the file name
	my $command;

	if( $v > 1 ) {
		$command = "convert -auto-orient -resample 96 -resize \"x640\" -strip $_ converted/$fname.png";
	} else {
		$command = "convert -resample 96 -resize \"640x\" -strip $_ converted/$fname.png";
	}
	open IN, "$command |" or die "no run: $!\n";

	close IN;
	print "converted : $fname\n";
}
Putting the above script into a folder, making the directory /converted, CD into the folder and

Code: Select all

perl exif_magick.pl
takes about 55 minutes to make 980 JPG of 5.5meg+, into resampled PNG of 500k on a AMD64, single-core, 3.8mgHz, 2-gig RAM on a USB external drive, Linux OS (Ubuntu 9.10), package-installs of ImageMagick, PerlMagick, ExifTool and their libs.

:? Can I gang the options in PerlMagick?? I found this is important to resample the dpi I seek, then do the image's new dimensions. I would like to rewrite the script as all PerlMagick, perhaps, to see if I can get even better performance.

User avatar
anthony
Posts: 8884
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Script CLI instead of PerlMagick

Post by anthony »

Sorry probably a little off topic. But I would suggest you use an array for your command, rather than a string. Saves you from needing a shell to parse the cting into a command and gives you more control.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply