Created
November 16, 2011 05:21
-
-
Save VienosNotes/1369345 to your computer and use it in GitHub Desktop.
Image spliter
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; | |
| mkdir "numbers"; | |
| mkdir "modified"; | |
| mkdir "concat"; | |
| for (@ARGV) { | |
| chop for 1..4; | |
| system "convert $_ -threshold 33000 ./modified/mod_$_" . ".bmp"; | |
| local $@; | |
| eval { | |
| img_split("./modified/mod_$_" . ".bmp"); | |
| }; | |
| print $@; | |
| } | |
| sub img_split { | |
| my $file = shift; | |
| my $test = Imager->new(file => "./numbers/0NZS42.jpg_num4.bmp"); | |
| print "split $file ..."; | |
| my $img = Imager->new(file => $file); | |
| chop $file for (1..4); | |
| substr($file, 0, 15) = ""; | |
| my $height = $img->getheight(); | |
| my $width = $img->getwidth(); | |
| my $threshold = $_[1] // 128; | |
| my $c_width = $_[2] // 5; | |
| my $c_height = $_[3] // 10; | |
| my @white; | |
| outer: | |
| for my $x (1..$width-1) { | |
| for (1..$height-1) { | |
| my @color = $img->getpixel(x => $x, y => $_)->rgba; | |
| next outer if $color[0] < $threshold; | |
| } | |
| push @white, $x; | |
| } | |
| my @cut; | |
| for (0..(scalar @white)-2) { | |
| if ($white[$_ + 1] - $white[$_] > $c_width) { | |
| push @cut, $img->crop(left => $white[$_] + 1, right => $white[$_ + 1]); | |
| } | |
| } | |
| scalar @cut == 5 or die "failed.\n"; | |
| for my $idx (0..(scalar @cut)-1) { | |
| my @white; | |
| outer: | |
| for my $y (1..$cut[$idx]->getheight-1) { | |
| for (1..$cut[$idx]->getwidth-1) { | |
| my @color = $cut[$idx]->getpixel(x => $_, y => $y)->rgba; | |
| next outer if $color[0] < $threshold; | |
| } | |
| push @white, $y; | |
| } | |
| for (0..(scalar @white)-2) { | |
| if ($white[$_ + 1] - $white[$_] > $c_height) { | |
| $cut[$idx] = $cut[$idx]->crop(top => $white[$_], bottom => $white[$_+1]); | |
| last; | |
| } | |
| } | |
| $cut[$idx]->write(file => "numbers/$file" . "_num$idx.bmp") or print "hoge"; | |
| } | |
| concat($file . ".tif", @cut); | |
| print "done. \n"; | |
| } | |
| sub concat { | |
| my $name = shift; | |
| my @list = @_; | |
| my $img = Imager->new(file => "./base.bmp"); | |
| my $x = 5; | |
| for (0..4) { | |
| $img->paste(src => $list[$_], left => $x, bottom => 18); | |
| $x += $list[$_]->getwidth + 10; | |
| } | |
| $img->write(file => "concat/$name"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment