| 1 | package Image::Fixup; | ||
| 2 | # Copyright (c) 2009 Christopher Davaz. All rights reserved. | ||
| 3 | # This program is free software; you can redistribute it and/or | ||
| 4 | # modify it under the same terms as Perl itself. | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use vars qw/$VERSION/; | ||
| 8 | |||
| 9 | use base qw/Class::Light/; | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | use Image::ExifTool; | ||
| 13 | use Image::Magick; | ||
| 14 | |||
| 15 | $VERSION = '0.01002'; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Image::Fixup - Provides methods to fixup images. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # Full script in t/scripts/fix_images | ||
| 24 | |||
| 25 | # Read the image | ||
| 26 | |||
| 27 | my $image = Image::Fixup->new($file); | ||
| 28 | |||
| 29 | # Print out image information | ||
| 30 | |||
| 31 | $image->printImageInfo; | ||
| 32 | |||
| 33 | # Change image orientation | ||
| 34 | |||
| 35 | $image->autoOrient; | ||
| 36 | |||
| 37 | # Resize the image | ||
| 38 | |||
| 39 | $image->autoResize; | ||
| 40 | |||
| 41 | # Write image to disk | ||
| 42 | |||
| 43 | $image->writeImage; | ||
| 44 | |||
| 45 | # Print out image information again | ||
| 46 | |||
| 47 | $image->printImageInfo; | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | I needed something that would automatically orient and resize my images. The | ||
| 52 | synopsis pretty much says it all. | ||
| 53 | |||
| 54 | =head1 TODO | ||
| 55 | |||
| 56 | Write documentation for individual methods. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | my %exif2im = ( | ||
| 61 | 'Horizontal (normal)' => 'top-left', | ||
| 62 | 'Mirror horizontal' => 'top-right', | ||
| 63 | 'Rotate 180' => 'bottom-right', | ||
| 64 | 'Mirror vertical' => 'bottom-left', | ||
| 65 | 'Mirror horizontal and rotate 270 CW' => 'left-top', | ||
| 66 | 'Rotate 90 CW' => 'right-top', | ||
| 67 | 'Mirror horizontal and rotate 90 CW' => 'right-bottom', | ||
| 68 | 'Rotate 270 CW' => 'left-bottom' | ||
| 69 | ); | ||
| 70 | |||
| 71 | my %im2exif = reverse %exif2im; | ||
| 72 | |||
| 73 | sub _init { | ||
| 74 | my $self = shift; | ||
| 75 | my $path = shift; | ||
| 76 | my $prefix = defined $_[0] ? shift : 'fixed_'; | ||
| 77 | my $image = Image::Magick->new; | ||
| 78 | my $exif = Image::ExifTool->new; | ||
| 79 | my ($base, $file) = ($path =~ /(.*)\/(.*)/); | ||
| 80 | $base = $path unless $base; | ||
| 81 | $file = $base unless $file; | ||
| 82 | |||
| 83 | my $err = $image->Read($path); | ||
| 84 | warn $err if $err; | ||
| 85 | $exif->ExtractInfo($path); | ||
| 86 | |||
| 87 | my %attr = ( | ||
| 88 | image => $image, | ||
| 89 | exif => $exif, | ||
| 90 | filename => $path, | ||
| 91 | outfile => $prefix . $file, | ||
| 92 | filesize => $image->Get('filesize'), # in bytes | ||
| 93 | orientation => __PACKAGE__->convertOrientation($exif->GetValue('Orientation')), | ||
| 94 | height => $image->Get('height'), | ||
| 95 | width => $image->Get('width') | ||
| 96 | ); | ||
| 97 | |||
| 98 | $self->{$_} = $attr{$_} for keys %attr; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub convertOrientation { | ||
| 102 | my $self = shift; | ||
| 103 | my $orient = shift; | ||
| 104 | return undef unless defined $orient; | ||
| 105 | if (exists $exif2im{$orient}) { | ||
| 106 | return $exif2im{$orient}; | ||
| 107 | } elsif (exists $im2exif{$orient}) { | ||
| 108 | return $im2exif{$orient}; | ||
| 109 | } | ||
| 110 | croak "Unknown orientation $orient"; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub autoOrient { | ||
| 114 | my $self = shift; | ||
| 115 | my $image = $self->getImage; | ||
| 116 | my $exif = $self->getExif; | ||
| 117 | $image->AutoOrient; | ||
| 118 | $exif->SetNewValue('Orientation','Horizontal (normal)'); | ||
| 119 | $self->{orientation} = 'top-left'; | ||
| 120 | $self->updateInfo; | ||
| 121 | } | ||
| 122 | |||
| 123 | # Currently just halves the size of the image. A better algorithm (but more | ||
| 124 | # resource intensive) would continually reduce the image size and check the | ||
| 125 | # resulting file size until the file size is below a specified limit. | ||
| 126 | sub autoResize { | ||
| 127 | my $self = shift; | ||
| 128 | my $image = $self->getImage; | ||
| 129 | $image->Resize( | ||
| 130 | width => int($image->Get('width') / 2), | ||
| 131 | height => int($image->Get('height') / 2) | ||
| 132 | ); | ||
| 133 | $self->updateInfo; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub updateInfo { | ||
| 137 | my $self = shift; | ||
| 138 | my $image = $self->getImage; | ||
| 139 | $self->{height} = $image->Get('height'); | ||
| 140 | $self->{width} = $image->Get('width'); | ||
| 141 | $self->{filesize} = $image->Get('filesize'); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub writeImage { | ||
| 145 | my $self = shift; | ||
| 146 | my $image = $self->getImage; | ||
| 147 | my $exif = $self->getExif; | ||
| 148 | my $out = $self->getOutfile; | ||
| 149 | $DB::single = 1; | ||
| 150 | $image->Write(filename => $out); | ||
| 151 | $exif->WriteInfo($out); | ||
| 152 | $self->_init($out,''); | ||
| 153 | } | ||
| 154 | |||
| 155 | sub setOrientation { | ||
| 156 | my $self = shift; | ||
| 157 | my $orient = shift; | ||
| 158 | $self->getImage->Set(orientation => $orient); | ||
| 159 | $self->{orientation} = $orient; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub printImageInfo { | ||
| 163 | my $self = shift; | ||
| 164 | print 'filename: ' . $self->getFilename . "\n"; | ||
| 165 | print 'orientation: ' . ($self->getOrientation || 'unknown') . "\n"; | ||
| 166 | print 'width: ' . $self->getWidth . "\n"; | ||
| 167 | print 'height: ' . $self->getHeight . "\n"; | ||
| 168 | print 'filesize: ' . $self->getFilesize . "\n"; | ||
| 169 | } | ||
| 170 | |||
| 171 | 1; | ||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | Christopher Davaz www.chrisdavaz.com cdavaz@gmail.com | ||
| 176 | |||
| 177 | =head1 VERSION | ||
| 178 | |||
| 179 | Version 0.01002 (Apr 25 2009) | ||
| 180 | |||
| 181 | =head1 COPYRIGHT | ||
| 182 | |||
| 183 | Copyright (c) 2009 Christopher Davaz. All rights reserved. | ||
| 184 | This program is free software; you can redistribute it and/or | ||
| 185 | modify it under the same terms as Perl itself. | ||
| 186 | |||
| 187 | =cut |