Skip to content

Instantly share code, notes, and snippets.

@kunalbalajeejha
Created May 5, 2013 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunalbalajeejha/5520808 to your computer and use it in GitHub Desktop.
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.
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";
}
@samba69
Copy link

samba69 commented May 18, 2014

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

@hopikiva
Copy link

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

@NGhacker
Copy link

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