While riding the train from Chicago to Detroit last night, I finally wrote a little program I have needed for a while. Up to know I have put black borders around images by firing up Photoshop, and as tiresome and annoying as that is, I was too lazy to do anything about it.
So, while I was uploading pics from my phone to my computer, I banged out this dirty little script to make the transformation. I want to go from the full size (640x480) images my phone provides to half size (320x240) images with a one pixel black border around them.
I wrote this in about the time it takes for me to start Photoshop and open an image, minus a few clean-ups to make it more presentable.
#!/usr/bin/perl
use strict;
use warnings;
use GD;
my( $file, $new_file ) = @ARGV;
my $source = GD::Image->newFromJpeg( $file );
die "Could not open [$file]\n" unless ref $source;
my( $width, $height ) = $source->getBounds();
my( $new_width, $new_height ) =
map { int($_ / 2) } ( $width, $height );
my $thumb = GD::Image->new( $new_width, $new_height );
$thumb->copyResized( $source, 0,0 => 0,0 =>
$new_width,$new_height => $width,$height );
black_border( $thumb );
open my( $fh ), "> $new_file"
or die "Could not write [$new_file]: $!\n";
print $fh $thumb->jpeg;
# # # # # # # # # # # # # # # # # # # # # # # # # # # #
sub black_border
{
my $image = shift;
my $black = $image->colorAllocate( 0, 0, 0 );
my( $w, $h ) = map { $_ - 1 } $image->getBounds();
foreach my $points (
[0,0 => 0,$h],
[0,$h => $w,$h],
[$w,$h => $w, 0],
[$w, 0 => 0, 0] )
{
$image->line( @$points, $black );
}
}
imagemagick (Score:1)
convert -resize 50% -border 1x1 -bordercolor black orig.img new.img
assuming you have Imagemagick installed. Substitute mogrify for convert if you don't want to keep the originals.
Re:imagemagick (Score:2)
Eventually this thing is going to end up in another program, but it's good to have the command line option too. Thanks
Re:imagemagick (Score:1)