Created
May 5, 2013 13:16
-
-
Save kunalbalajeejha/5520808 to your computer and use it in GitHub Desktop.
Perl script to convert the National Geographic Magazine CNG file to JPG file and then converting the group of files in a folder to PDF document.
This file contains 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
use strict; | |
use warnings; | |
use PDF::API2::Lite; | |
my $basedir = "/Volumes/SEAGATE/NGM/NGM/200x"; | |
my $ouputdir = "/Volumes/SEAGATE/NGM/Magazine"; | |
process_files($basedir); | |
print "Completed.\n"; | |
sub process_files { | |
my $path = shift; | |
opendir (DIR, $path) | |
or die "Unable to open $path: $!"; | |
my @files = grep { !/^\.{1,2}$/ } readdir (DIR); | |
closedir (DIR); | |
@files = map { $path . '/' . $_ } @files; | |
my $flag = 0; | |
for my $listitem (@files) { | |
if (-d $listitem) { | |
process_files ($listitem); | |
} else { | |
convertCNG2JPG($listitem); | |
$flag = 1; | |
} | |
} | |
if ($flag==1) | |
{ | |
convertToPDF($path); | |
} | |
} | |
sub convertCNG2JPG { | |
my $file = shift; | |
if($file =~ /cng$/){ | |
#print "Converted Filename : $file\n"; | |
my $ofile = (split(/\./,$file))[0].".jpg"; | |
#print "Converted Filename : $ofile\n"; | |
my $isValidCNG = 0; | |
open OUTPUTFILE, ">", $ofile or die $!; | |
binmode OUTPUTFILE; | |
open INPUTFILE, "<", $file or die $!; | |
binmode INPUTFILE; | |
local $/ = undef; | |
$/ = \(2**16); # ignore line breaks, read in 1 MiB chunks | |
while (<INPUTFILE>) { | |
$_ ^= "\xEF" x length; | |
die "Not a valid CNG file" if $. == 1 and not /^.{6}(JFIF|Exif)/s; | |
print OUTPUTFILE; | |
} | |
close INPUTFILE; | |
close OUTPUTFILE; | |
} | |
} | |
sub convertToPDF{ | |
my $path = shift; | |
chdir($path); | |
my @tmp = split(/\//,$path); | |
my $tmpname = $tmp[scalar(@tmp)-1]; | |
my $filename = (substr $tmpname, 0, 4)."_".(substr $tmpname, 4, 2)."_".(substr $tmpname, 6, 2).".pdf"; | |
#print "Filename : $filename\n"; | |
opendir my $dh, $path | |
or die "Can't opendir '$path': $!"; | |
my @jpg = grep -f "$path/$_" && /\.jpe?g/i, readdir $dh; | |
closedir $dh | |
or die "Can't closedir '$path': $!"; | |
my $pdf = PDF::API2::Lite->new; | |
my $counter = 0; | |
for my $jpg ( sort @jpg ) { | |
my $filesize = (-s "$path/$jpg") / 1024; | |
#print "Size: $filesize\n"; | |
if ($filesize < 1024) { | |
my $image = $pdf->image_jpeg( "$path/$jpg" ); | |
$pdf->page( $image->width, $image->height ); | |
$pdf->image( $image, 0, 0 );$counter++; | |
} | |
} | |
$pdf->saveas("$ouputdir/$filename"); | |
print "Created File : $filename. with Pages : $counter\n"; | |
} |
me too but have a slew of cng files to convert. I wish you'd come back and tell us poor fools more about what we need to do in order to get it done... hope to see you soon... thanks
I've got a tutorial on how to do this with a different code and Windows 7:
http://persuasivegraffiti.weebly.com/random/how-to-convert-national-geographic-cng-files-to-pdf
Hope this helps!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello there,
I am clueless about perl and would like to ask how to use this please? I want to convert my nat geo so I can view i on my tablet.
Thank you very much