[Oc-pm] 2016 April Challange

Vijay Anand vijay2 at cox.net
Tue Apr 19 19:49:11 PDT 2016


I struggled with Image::Magick, then gave up on it, and went with GD.

That seems to have worked - the pictures are : A cat, A Parrot, and 2 
penguins.
The colors look a little weird, so I probably have a bug.

Here is the code:

use strict;
use warnings;
use GD;
###############################
# April Challange
## Reconstruct JPG images one pixel at a time, from JSON.
### By Vijay Anand
#################################################
my $inp = shift @ARGV or die "NO file specified";
my $out = $inp . ".jpg";
local $/;
open my $f, "<", $inp or die "Cannot open $inp:$!";
my $str=<$f>;
close $f;
print "Read ",length($str)," bytes\n";

$str=~s/:/=>/g;
my   $j   = eval $str;
print "Converted to perl.", my $rows=scalar(keys(%$j)), " Rows, ",
      my $cols=scalar(keys(%{$j->{'0'}})), " Columns\n";

my $image = GD::Image::->new($cols,$rows) or die "Cannot create GD 
object"; # width,height
my $black = $image->colorAllocate(0,0,0);
my $red = $image->colorAllocate(255,0,0);
$image->rectangle(0,0,$cols -1,$rows -1,$black); # Put a black frame 
around the picture
$image->fill($cols-2,$rows-2,$red);


my $pxcount = 0;
for my $r(keys %$j){
    for my $c(keys %{$j->{$r}}){
           my $px = $j->{$r}{$c};
$image->setPixel($c,$r,$image->colorResolve(@$px{qw|RED GREEN BLUE|}));
           $pxcount++;
    }
}
print "$pxcount pixels color set\n";
open my $o, ">", $out or die "Cannot open $out:$!";
binmode $o;
print $o $image->jpeg(90);
close $o;
print "Processed $out. Created $out \n";




More information about the Oc-pm mailing list