Created
October 12, 2013 21:14
-
-
Save hernan604/6955005 to your computer and use it in GitHub Desktop.
generate one tiled image from multiple images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Imager::Montage; | |
use Data::Printer; | |
my $imgs =# <*.*>; | |
[ | |
'11.JPG', | |
'14.JPG', | |
'2.JPG', | |
'3.JPG', | |
'5.JPG', | |
'DSC03304.JPG', | |
'DSC03316.JPG', | |
'DSC03397.JPG', | |
]; | |
*Imager::Montage::gen_page = sub { | |
my $self = shift; | |
my $args = shift; | |
$args->{geometry_w} ||= $args->{resize_w}; | |
$args->{geometry_h} ||= $args->{resize_h}; | |
$args->{$_} ||= 0 | |
for(qw/border frame margin_v margin_h/); | |
$args->{$_} ||= '#ffffff' | |
for (qw/background_color border_color frame_color/); | |
$args->{page_width} | |
||= $args->{frame} * 2 | |
+ ( $args->{border} * 2 ) * $args->{cols} | |
+ $args->{geometry_w} * $args->{cols} | |
+ ( $args->{margin_h} * 2 ) * $args->{cols}; | |
$args->{page_height} | |
||= $args->{frame} * 2 | |
+ ( $args->{border} * 2 ) * $args->{rows} | |
+ $args->{geometry_h} * $args->{rows} | |
+ ( $args->{margin_v} * 2 ) * $args->{rows}; | |
$args->{$_} = $self->_load_color( $args->{$_} ) | |
for (qw/background_color border_color frame_color/); | |
# create a page | |
my $page_img = Imager->new( | |
xsize => $args->{page_width}, | |
ysize => $args->{page_height}); | |
$self->_set_resolution( $page_img, $args->{res} ) | |
if ( exists $args->{res} ); | |
# this could make a frame for page | |
if ( exists $args->{frame} ) { | |
$page_img->box( | |
filled => 1, | |
color => $args->{frame_color} ); | |
my $box = Imager->new( | |
xsize => $args->{page_width} - $args->{frame} * 2 , | |
ysize => $args->{page_height} - $args->{frame} * 2 )->box( filled => 1, color => $args->{background_color}); | |
$page_img->paste( | |
left => $args->{frame}, | |
top => $args->{frame}, | |
src => $box); | |
} | |
else { | |
$page_img->box( | |
filled => 1, | |
color => $args->{background_color}, | |
); | |
} | |
my ( $top, $left ) = ( | |
$args->{frame}, | |
$args->{frame} ); | |
for my $col ( 0 .. $args->{cols} - 1 ) { | |
$top = $args->{frame}; | |
for my $row ( 0 .. $args->{rows} - 1 ) { | |
# get filename | |
my $file = ${ $args->{files} }[ $col * $args->{rows} + $row ]; | |
next if ( ! defined $file ); | |
my $canvas_img = $self->_load_image($file); | |
# resize it if we define a new size | |
if ( exists $args->{resize_w} ) { | |
$canvas_img = $canvas_img->scale( | |
xpixels => $args->{resize_w}, | |
ypixels => $args->{resize_h}, | |
type => 'max',); } # XXX: make nonprop as parameter | |
if ( exists $args->{ crop_h } && exists $args->{ crop_w } ) { | |
$canvas_img = $canvas_img->crop( | |
width => $args->{ crop_w }, | |
height => $args->{ crop_h }, | |
) or return $canvas_img->errstr; | |
} | |
# flip | |
if ( exists $args->{flip} | |
and ( exists $args->{flip_exclude} and !eval( $args->{flip_exclude} ) ) ) { | |
$canvas_img->flip( dir => $args->{flip} ); } | |
# if border is set | |
if( $args->{border} ) { | |
# gen border , paste it before we paste image to the page | |
my $box = Imager->new( | |
xsize => $args->{geometry_w} + $args->{border} * 2, | |
ysize => $args->{geometry_h} + $args->{border} * 2 )->box( filled => 1, color => $args->{border_color} ); | |
$page_img->paste( | |
left => $left + $args->{margin_h} , | |
top => $top + $args->{margin_v} , | |
src => $box ); | |
} | |
$page_img->paste( | |
left => $left + $args->{margin_h} + $args->{border} , # default border is 0 | |
top => $top + $args->{margin_v} + $args->{border} , | |
src => $canvas_img); | |
} continue { | |
$top += ( $args->{border} * 2 + $args->{margin_v} * 2 + $args->{geometry_h} ); | |
} | |
} | |
continue { | |
$left += ( $args->{border} * 2 + $args->{margin_h} * 2 + $args->{geometry_w} ); | |
} | |
return $page_img; | |
}; | |
my $im = Imager::Montage->new; | |
my $page = $im->gen_page( | |
{ | |
files => $imgs, | |
crop_h => 130, | |
crop_w => 140, | |
resize_w => 180, | |
# resize_h => 50, | |
margin_v => 10, | |
margin_h => 10, | |
geometry_w => 130, # geometry from source. if not set , the resize_w , resize_h will be the default | |
geometry_h => 120, # if we aren't going to resize the source images , we should specify the geometry at least. | |
cols => 5, | |
rows => 2, | |
page_width => 900, | |
page_height => 400, | |
background_color => "#FFF", | |
} | |
); | |
$page->write( file => 'page.png' , type => 'png' ); # generate a 1000x1000 pixels image with 5x5 tiles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment