Wednesday, 25 August 2010

Android phone picture shrinker for publishing to picasaweb

An old script still proving useful. I don't want to break my Picasa storage limit just yet. This one puts 800x600 photos up onto the tinterweb, and stores the full size images locally on my NFS share /photos/phone_photos_.

Please adjust your settings according before using it. Requires PerlMagick and Google command line.

Script can also be downloaded here.




#!/usr/bin/perl -w

##############################################################################
# syncshrink Version .05 #
# Copyright 2010 tng@chegwin.org All rights reserved #
# Created 3-Jun-2010 Last Modified 25-Aug-2010 #
##############################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


use strict;
use Image::Magick;
use File::Copy;

#3264 2448

my $portrait_columns="2448";
my $landscape_columns="3264";

my ($sec,$min,$hour,$mday,$month,$year,
$wday,$yday,$isdst) = localtime time;

my $directory_write_to="phone_photos_";

my $image_directory="/media/08A2-BE0A/DCIM/Camera/";

$month++;
$year+=1900;

my $picasa_upload_name="$mday"."_$month"."_$year";
my $write_to_directory="/photos/$directory_write_to"."$picasa_upload_name";
my $upload_directory="/tmp/picasa_upload/";

exit "Sorry, no phone found $!" unless (-e "$image_directory");
exit "Sorry, no output directory made $!" unless (mkdir ("$write_to_directory",0777));
exit "Sorry, no upload directory made $!" unless (mkdir ("$upload_directory",0777));


opendir (IMG_DIR,"$image_directory") or die "couldn't open ./ $!";
foreach (sort(readdir(IMG_DIR))){
my $file=$_;
next unless /\.JPG/i;
next if /_sm.jpg/;
next if /_med.jpg/;

print ("$_\n");
my $sm_filename=$file;

$sm_filename =~ s/\.jpg//g;
$sm_filename =~ s/\.JPG//g;
my $pic_name=$sm_filename;
my $med_filename=$sm_filename;
$med_filename .= "_med.jpg";

print ("Creating $med_filename");

my $p = new Image::Magick;
my $image = new Image::Magick;
$image->Read("$image_directory/$file");

my ($current_columns,$current_rows)=$image->Get('base-columns','base-rows');

print ("$current_columns\t $current_rows\n");

if ($current_columns <= $portrait_columns){

print ("portrait\n");
$image->Scale(geometry => '600x800');

} elsif ($current_columns >> $landscape_columns){

print ("landscape\n");
#If landscape then create 640 x 480 and
#100 x 75
$image->Scale(geometry => '800x600');

} else {

print ("Something is wrong - this image does not have the correct number of columns");
$image->Scale(geometry => '800x600');

}
$image->Write("$upload_directory/$med_filename");
move ("$image_directory/$file","$write_to_directory/$file");


}
closedir (IMG_DIR);


print ("\n Attempting to run: google picasa create --title $picasa_upload_name $upload_directory\n");
open (UPLOAD_GOOGLE,"google picasa create --title $picasa_upload_name $upload_directory/* |") or die "Cannot upload t
o picasa $!";
close (UPLOAD_GOOGLE);

No comments: