#!/usr/bin/perl # # bdtouch # # A program which dempostratew the usage of the Imager module # by Michele Beltrame # Licensed under the GPL: http://www.gnu.org/copyleft/gpl.html use strict; use warnings; use utf8; use Getopt::Long; use Imager; our $VERSION = '1.2.0'; # Get command line parameters my %args; GetOptions( \%args, 'src=s', 'tgt=s', 'action=s', 'verse=s', 'size=i', 'degrees=f', 'left=i', 'top=i', 'width=i', 'height=i', 'intensity=f', 'stddev=f', ); # Ensure we have at least the base parameters if ( !$args{src} || !$args{tgt} ) { print "Usage: bdtouch.pl --src=source_file --tgt=target_file [ --action=action ]\n"; exit; } # Initialize Imager object my $img = Imager->new(); # Open source file $img->open( file => $args{src} ) or die "Error opening source file: ".$img->errstr; # See if an action is specified, and perform it in that case. # If there is none, we assume we just want to convert format. my $newimg = $args{action} eq 'resize' ? resize($img, \%args) : $args{action} eq 'rotate' ? rotate($img, \%args) : $args{action} eq 'flip' ? flip($img, \%args) : $args{action} eq 'crop' ? crop($img, \%args) : $args{action} eq 'contrast' ? contrast($img, \%args) : $args{action} eq 'blur' ? blur($img, \%args) : !$args{action} ? $img : do { print "Please specify one action (resize, rotate, flip, crop, contrast, blur), or no action at all to just convert between formats.\n"; exit } ; # Save destination file $newimg->write( file => $args{tgt} ) or die 'Error saving file: '.$img->errstr; # ##### Filter functions ##### # Resize to a definite size sub resize { my ($img, $args) = @_; # Test options passed on the command line if ( (!exists $args->{verse}) || (!exists $args->{size}) ) { print "Resize parameters: --verse=scaling_verse (h or v) --size=new_size\n"; exit; } my $newimg = $img->scale( "$args->{verse}pixels" => $args->{size} ); return $newimg; } # Rotate of specified degrees sub rotate { my ($img, $args) = @_; # Test options passed on the command line if ( (!exists $args->{degrees}) ) { print "Rotate parameters: --degrees=rotation_degrees"; exit; } my $newimg = $img->rotate( degrees => $args->{degrees} ); return $newimg; } # Flip image, horizontally or vertically sub flip { my ($img, $args) = @_; # Test options passed on the command line if ( (!exists $args->{verse}) || (!exists $args->{size}) ) { print "Flip parameters: --verse=flipping_verse (h or v)\n"; exit; } my $newimg = $img->flip( dir => $args->{verse} ); return $newimg; } # Transform: crop sub crop { my ($img, $args) = @_; # Test options passed on the command line if ( (!exists $args->{left}) || (!exists $args->{top}) || (!exists $args->{width}) || (!exists $args->{height}) ) { print "Flip parameters: --left=corner_left --top=corner_top --width=crop_width --height=crop_height\n"; exit; } my $newimg = $img->crop( left => $args->{left}, top => $args->{top}, width => $args->{width}, height => $args->{height}, ); return $newimg; } # Contrast sub contrast { my ($img, $args) = @_; # Test options passed on the command line if ( !exists $args->{intensity} ) { print "Contrast parameters: --intensity=intensity\n"; exit; } my $newimg = $img->filter( type => 'contrast', intensity => $args->{intensity}, ); return $newimg; } # Filter: blur sub blur { my ($img, $args) = @_; # Test options passed on the command line if ( !exists $args->{stddev} ) { print "Contrast parameters: --intensity=intensity\n"; exit; } my $newimg = $img->filter( type => 'gaussian', stddev => $args->{stddev}, ); return $newimg; }