-
-
Save pjf/4417477 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /usr/bin/perl -w | |
use 5.010; | |
use strict; | |
use warnings; | |
use threads; | |
use Thread::Queue; | |
use autodie qw(:all); | |
use Image::ExifTool qw(ImageInfo); | |
use Data::Dumper; | |
use Imager; | |
use File::Slurp qw(read_file); | |
# Imager needs to be preloaded if we're using threads. | |
Imager->preload; | |
# Set up our work queue. Right now it's just @ARGV. | |
my $work_queue = Thread::Queue->new; | |
$work_queue->enqueue(@ARGV); | |
$work_queue->end; | |
# Spawn our threads, each of which will process files until we're done. | |
my @threads; | |
my $cores = get_cores(); | |
for (1..$cores) { | |
push(@threads, | |
threads->create( sub { | |
while (my $src = $work_queue->dequeue) { | |
process_image($src); | |
} | |
}) | |
); | |
} | |
# Join threads. | |
foreach my $thread (@threads) { $thread->join; } | |
sub process_image { | |
my ($raw) = @_; | |
my ($new) = $raw =~ m{(.*).CR2$}i; | |
next if not $new; # Skip non-CR2 files | |
$new .= ".jpg"; | |
say "$raw -> $new..."; | |
my $exif = ImageInfo($raw, [qw(PreviewImage Orientation)], { Binary => 1 }); | |
my ($rotation) = ( $exif->{Orientation} =~ /(\d+)/ ); | |
my $img = Imager->new(); | |
$img->read(data => ${$exif->{PreviewImage}}) | |
or die $img->errstr; | |
$img | |
->scale(type=>'min', xpixels=>2048, ypixels=>2048) | |
->rotate(degrees => $rotation) | |
->write(file=>$new, type=>'jpeg') | |
; | |
return; | |
} | |
sub get_cores { | |
my $cpuinfo = read_file("/proc/cpuinfo"); | |
my $max_threads; | |
# Walk through all the cores to figure out how many we have. | |
while ($cpuinfo =~ m{^processor\s*:\s*(?<cores>\d+)}msgi ) { | |
$max_threads = $+{cores}+1; | |
}; | |
return $max_threads; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use:
exifpic.pl *.CR2