| 1 | #!/usr/bin/perl |
|---|
| 2 | # -*- coding:utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Author: FUJIWARA Shunichiro <fujiwara.shunichiro at gmail.com> |
|---|
| 5 | # License: Same as Perl |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | use strict; |
|---|
| 9 | use Imager; |
|---|
| 10 | use Imager::Fill; |
|---|
| 11 | use Imager::Font; |
|---|
| 12 | use List::Util qw/ min max /; |
|---|
| 13 | use Getopt::Long; |
|---|
| 14 | |
|---|
| 15 | my ( $date, $usage ); |
|---|
| 16 | GetOptions( |
|---|
| 17 | "date=s" => \$date, |
|---|
| 18 | "help|usage" => \$usage, |
|---|
| 19 | ); |
|---|
| 20 | my ( $source_file, $result_file ) = @ARGV; |
|---|
| 21 | |
|---|
| 22 | if ( !defined $source_file || !defined $result_file || $usage ) { |
|---|
| 23 | usage(); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | my $img = Imager->new; |
|---|
| 27 | $img->read( file => $source_file ) |
|---|
| 28 | or die $img->errstr; |
|---|
| 29 | |
|---|
| 30 | $img->filter( type => "contrast", intensity => 0.7 ); |
|---|
| 31 | $img->filter( type => "conv", coef => [ 0, 1, 0.2 ] ); |
|---|
| 32 | $img = solid_border( $img, "#bbbbbb" ); |
|---|
| 33 | |
|---|
| 34 | my $fill = Imager::Fill->new( solid => random_color('413626'), combine => 'add' ); |
|---|
| 35 | $img->box( fill => $fill ); |
|---|
| 36 | |
|---|
| 37 | $img = add_date( $img, $date ) if $date; |
|---|
| 38 | |
|---|
| 39 | $img->filter( |
|---|
| 40 | type => 'noise', |
|---|
| 41 | amount => 10, |
|---|
| 42 | subtype => 1, |
|---|
| 43 | ); |
|---|
| 44 | |
|---|
| 45 | $img->write( file => $result_file, type => 'jpeg' ) |
|---|
| 46 | or die $img->errstr; |
|---|
| 47 | |
|---|
| 48 | # solid_border is based on Imager/samples/border.pl |
|---|
| 49 | # http://search.cpan.org/~tonyc/Imager/samples/border.pl |
|---|
| 50 | sub solid_border { |
|---|
| 51 | my ( $source, $color ) = @_; |
|---|
| 52 | |
|---|
| 53 | my $w = $source->getwidth(); |
|---|
| 54 | my $h = $source->getheight(); |
|---|
| 55 | my $border = int ( max( $w * 0.015, $h * 0.015, 5 ) ); |
|---|
| 56 | my $out = Imager->new( |
|---|
| 57 | xsize => $source->getwidth() + 2 * $border, |
|---|
| 58 | ysize => $source->getheight() + 2 * $border, |
|---|
| 59 | bits => $source->bits, |
|---|
| 60 | channels => $source->getchannels, |
|---|
| 61 | ); |
|---|
| 62 | |
|---|
| 63 | $out->box( filled => 1, color => $color ) |
|---|
| 64 | or die "Invalid color '$color':", $out->errstr, "\n"; |
|---|
| 65 | |
|---|
| 66 | $out->paste( |
|---|
| 67 | left => $border, |
|---|
| 68 | top => $border, |
|---|
| 69 | img => $source, |
|---|
| 70 | ); |
|---|
| 71 | |
|---|
| 72 | return $out; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | sub random_color { |
|---|
| 76 | my $code = shift; |
|---|
| 77 | my @color = map { hex $_ } ( $code =~ m{(..)}g ); |
|---|
| 78 | |
|---|
| 79 | for my $c ( @color ) { |
|---|
| 80 | $c *= ( 1 + ( rand() > 0.5 ? +1 : -1 ) * rand(0.15) ); |
|---|
| 81 | $c = min( $c, 255 ); |
|---|
| 82 | $c = max( $c, 0 ); |
|---|
| 83 | } |
|---|
| 84 | $code = sprintf('#%02x%02x%02x', @color); |
|---|
| 85 | return $code; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | sub add_date { |
|---|
| 89 | my $img = shift; |
|---|
| 90 | my $date = shift; |
|---|
| 91 | $date =~ s/[^ \d\-\.]//g; |
|---|
| 92 | |
|---|
| 93 | my $color = Imager::Color->new('#ffaa33'); |
|---|
| 94 | my $ttfont = Imager::Font->new( |
|---|
| 95 | file => '7barPBd.TTF', # http://www.trojanbear.net/omake.htm#sevenbarb |
|---|
| 96 | ); |
|---|
| 97 | my $size = int( ($img->getwidth + $img->getheight) * 0.02 ); |
|---|
| 98 | $img->align_string( |
|---|
| 99 | font => $ttfont, |
|---|
| 100 | text => $date, |
|---|
| 101 | x => int( $img->getwidth - $size * 1.5 ), |
|---|
| 102 | y => int( $img->getheight - $size * 1.5 ), |
|---|
| 103 | halign => 'right', |
|---|
| 104 | valign => 'bottom', |
|---|
| 105 | size => $size, |
|---|
| 106 | color => $color, |
|---|
| 107 | aa => 1, |
|---|
| 108 | ); |
|---|
| 109 | return $img; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | sub usage { |
|---|
| 113 | print <<_END_ |
|---|
| 114 | Usage: |
|---|
| 115 | showanize.pl [ --date DATE_STRING ] SOURCE_FILE RESULT_FILE |
|---|
| 116 | |
|---|
| 117 | _END_ |
|---|
| 118 | ; |
|---|
| 119 | exit; |
|---|
| 120 | } |
|---|