#!/usr/bin/perl # $Id: imgmerge.pl,v 1.0.0-0 2011/03/25 11:29:02 Cnangel Exp $ use strict; use warnings; use vars qw/$starttime %ARGV/; BEGIN { $starttime = (times)[0] + (times)[1]; } END { printf("%d\n", ((times)[0] + (times)[1] - $starttime) * 1000) if ($ARGV{debug}); } use Getopt::Long qw(:config no_ignore_case); use Pod::Usage; use File::Find; use FindBin qw/$Bin/; use lib "$Bin/../lib"; use Conf::Libconfig; use Image::Magick; use Encode; my $man = 0; my $help = 0; pod2usage() if (scalar @ARGV == 0); GetOptions ( "c|conf=s" => \$ARGV{'conf'}, "i|infile=s" => \$ARGV{'infile'}, "o|outfile=s" => \$ARGV{'outfile'}, "d|dir=s" => \$ARGV{'dir'}, "debug" => \$ARGV{'debug'}, "verbose" => \$ARGV{'verbose'}, 'help|?' => \$help, man => \$man ) or pod2usage(2); pod2usage(1) if $help; pod2usage(-exitstatus => 0, -verbose => 2) if $man; pod2usage() unless (-f $ARGV{'conf'}); use Data::Dumper; my @files; my $img; my $capimg; my ($x) = (''); my %IMG_CHARACTER; my $conf = new Conf::Libconfig; $conf->read_file($ARGV{'conf'}); my $img_character_file = $conf->lookup_value("application.img_character_config"); die "Can't open file $img_character_file: $!" unless (-f $img_character_file); my $imagedir = $conf->lookup_value("application.img_datadir"); die "Can't find dir $imagedir: $!" unless (-d $imagedir); my $imageoutdir = $conf->lookup_value("application.img_dataoutdir"); $imageoutdir = "out" unless ($imageoutdir); mkdir($imageoutdir, 0777) unless (-d $imageoutdir); $img = new Image::Magick(); $x = $img->ReadImage('img/512x120_19321535.jpg'); warn "$x" if "$x"; #$capimg = new Image::Magick(); #$x = $capimg->Set( #size => '100x100', #alpha => 'Transparent', #); #$capimg->ReadImage('NULL:white'); #warn "$x" if "$x"; #$x = $capimg->Write( #filename => "image.png", #compression => 'None' #); #warn "$x" if "$x"; #exit; open my $fp, '<', $img_character_file or die "Can't read the file: $!"; while (my $line = <$fp>) { $line =~ s/^\s*//; $line =~ s/\s*$//; next unless ($line); my ($word, $filename) = split /\s+/, $line; Encode::from_to($word, "GBK", "UTF-8"); $IMG_CHARACTER{$filename} = $word; } close($fp); my $ret = &GetFileFromDir($imagedir); foreach my $file (@$ret) { my ($filename) = $file =~ /img\/([^\/]+)$/; next if ($filename =~ /^512x120/); if (defined $IMG_CHARACTER{$filename}) { print $filename . "\n"; my $tmpimg = $img->Clone(); $capimg = new Image::Magick(); $x = $capimg->Set( size => '100x100', alpha => 'Transparent', ); warn "$x" if "$x"; $x = $capimg->ReadImage($file); warn "$x" if "$x"; $x = $capimg->FloodfillPaint( fuzz => "5%", ); warn "$x" if "$x"; $x = $capimg->Transparent( color => 'black', ); warn "$x" if "$x"; $x = $tmpimg->Composite( image => $capimg, x => 75, y => 10, compose => 'over', geometry => '+35+65' ); warn "$x" if "$x"; $x = $tmpimg->Annotate( x => 190, y => 80, font => '/u/fonts/simkai.ttf', pointsize => 48, fill => 'green', text => $IMG_CHARACTER{$filename}, ); warn "$x" if "$x"; $x = $tmpimg->Write( filename => "$imageoutdir/$filename", compression => 'None' ); warn "$x" if "$x"; undef $capimg; } } undef $img; sub GetFileFromDir { my $dir = shift; find(\&wanted, $dir); return \@files; } sub wanted { push @files, $File::Find::name if (-f $_); } __END__ =head1 NAME imgmerge.pl - IMAGE MERGE TOOL =head1 SYNOPSIS imgmerge.pl -c imgmerge.pl -help =head1 OPTIONS =over 8 =item B<-c|--conf> Configuration for the script, default is $PROJECT_HOME/conf. =item B<-d|--dir> Directory about data files. =item B<-i|--infile> Input file about items. =item B<-o|--outfile> Output file about items. =item B<--debug> Debug mode, you can see how much time spent in B. =item B<--help> Print a brief help message and exits. =item B<--man> Prints the manual page and exits. =back =head1 DESCRIPTION B will read the given input file(s) and do something useful with the contents thereof, then output result of file(s). =head1 AUTHOR B (I) =head1 HISTORY I<2011/03/25 11:30:35> Builded. =cut