Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Created April 25, 2011 19:46
Show Gist options
  • Save awwaiid/941066 to your computer and use it in GitHub Desktop.
Save awwaiid/941066 to your computer and use it in GitHub Desktop.
Linux Format Perl Redone
#!/usr/bin/perl
use strict;
use Image::Magick;
# auxiliary variables
my $img_id = 0; #loop index
my @ordered_squares;
my $rotation = 180;
#Width and Height of each box, squares per side
my $box_width = 300;
my $box_height = $box_width;
my $square_dist = 100 + $box_width;
my $squares_per_side = 4;
my %special_squares = ( #first field is the position
0 => "NO\tGo\tGood Luck", # in the square sequence
3 => "NO\tChance\ttake yours",
6 => "NO\tFree Parking\t",
9 => "NO\tJail\ttoo bad!"
);
# Create and prepare the big, white background image
my $board_size = ($box_width + $square_dist)*$squares_per_side;
my ($image, $current_x) = Image::Magick->new;
$image->Set(size => $board_size . 'current_x' . $board_size);
$image->ReadImage("xc:white");
# Read picture names, captions and other info from the data files,
# inserting the special squares in the right places
open my $picture_list, '<', 'pictures_data.txt' or die "Cannot open pictures data file!\n";
while (<$picture_list>) {
if (defined ($special_squares{$img_id})) {
push @ordered_squares, $special_squares{$img_id} ;
$img_id++;
}
chomp;
push @ordered_squares, $_;
$img_id++;
}
close $picture_list;
# now add all the squares to the board
#initial coordinates
my $current_x = 100;
my $current_y = 100;
for ($img_id = 0; $img_id <= $#ordered_squares; $img_id++) {
my ($pic_name, $caption, $price) = split "\t", $ordered_squares[$img_id];
# draw a box, add the captions in it and save it in /tmp/tmp_image.jpg
draw_border_add_captions($box_width, $box_height, $caption, $price);
# if there is a current picture, resize it and put it
# at the center of the temporary image
insert_picture($pic_name, $box_width - 40, $box_height - 120) unless $pic_name =~ m/^NO$/;
my $tmp_image;
$tmp_image = Image::Magick->new;
$tmp_image->ReadImage("/tmp/tmp_image.jpg");
$tmp_image->Rotate( degrees => $rotation ) if $rotation > 0;
# place the current square at the right position on the board
$image->Composite( image => $tmp_image, compose => 'over', geometry => "+$current_y+$current_x");
#calculate new rotation value and origin of the next square;
# if index is a multiple of $squares_per_side
# we've reached a corner of the board, let's increase image rotation
$rotation += 90 if (($img_id > 0 ) && ( $img_id % ( $squares_per_side - 1) == 0));
$rotation -= 360 if ($rotation >= 360);
if($rotation == 180) {
$current_y = $current_y + $square_dist ;
} elsif ($rotation == 270) {
$current_x = $current_x + $square_dist;
} elsif ($rotation == 0) {
$current_y = $current_y - $square_dist;
} elsif ($rotation == 90) {
$current_x = $current_x - $square_dist;
} else {
die "wrong Rotation value: $rotation\n";
}
}
# add central slogan and save
$image->Annotate(
text => 'Linux Format rules!',
stroke => "red",
pointsize => 90,
rotate => 215,
current_x => int($board_size*0.4),
y => int($board_size*0.4)
);
$image->write("jpg:board.jpg");
sub draw_border_add_captions {
my ($box_width, $box_height, $caption, $price) = @_;
my $tmp_image;
$tmp_image = Image::Magick->new;
$tmp_image->Set( size => "${box_width}current_x$box_height" );
$tmp_image->ReadImage("xc:white");
my $x_line_start = 10;
my $y_line_start = 10;
my $x_line_end = 10;
my $y_line_end = $box_width -10;
$tmp_image->Draw(
stroke => "red",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
my $x_line_start = 10;
my $y_line_start = $box_width - 10;
my $x_line_end = $box_height - 10;
my $y_line_end = $box_width - 10;
$tmp_image->Draw(
stroke => "blue",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
my $x_line_start = $box_height - 10;
my $y_line_start = $box_width - 10;
my $x_line_end = $box_height - 10;
my $y_line_end = 10;
$tmp_image->Draw(
stroke => "green",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
my $y_line_start = 10;
my $x_line_start = $box_height - 10;
my $x_line_end = 10;
my $y_line_end = 10;
$tmp_image->Draw(
stroke => "red",
primitive => "line",
points => "$y_line_start,$x_line_start $y_line_end,$x_line_end"
);
#add Caption and price to temporary image
$tmp_image->Annotate( text => $caption, stroke =>"blue", pointsize=> 24, current_x => 20, y => 10 + int($box_height * 0.75 + 20));
$tmp_image->Annotate( text => $price, stroke =>"red", pointsize=> 16, current_x => 40, y => 10 + int($box_height * 0.75 + 40));
$tmp_image->write("jpg:/tmp/tmp_image.jpg");
}
sub insert_picture {
my ($pic_name, $width, $height) = @_;
my $current_pic = Image::Magick->new;
$current_pic->ReadImage($pic_name);
$current_pic->Resize(width => $width, height => $height);
my $tmp_image;
$tmp_image = Image::Magick->new;
$tmp_image->ReadImage("/tmp/tmp_image.jpg");
#put the current picture at the center of the temporary image
$tmp_image->Composite(image => $current_pic, compose => 'over', geometry => "+20+20");
$tmp_image->write("jpg:/tmp/tmp_image.jpg");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment