#!/usr/local/bin/perl use strict; use Image::Magick; use Image::Magick::Tiler; # ディレクトリを指定して、その直下にある画像ファイルをリサイズする(出力はjpg固定、変更時はソースを修正すること) # このplファイルと指定するディレクトリは同一階層にあること my @DIRS = (); # リサイズ後の画像サイズ my $RESIZE_X = 480; my $RESIZE_Y = 854; # 見開きのページの際に、左ページ→右ページの順で読む場合は0、右ページ→左ページの順で読む場合は1 my $READ_DIRECTION = 0; # 横のサイズが縦のサイズの何倍以上になったら縦に2分割する対象であると解釈するか my $WIDTH_PER_HEIGHT = 1.3; foreach my $dir(@DIRS) { my ($SEC, $MIN, $HOUR, $DAY, $MONTH, $YEAR) = localtime(); $YEAR += 1900; $MONTH++; my $dir_time_name = sprintf("%04d%02d%02d_%02d%02d%02d", $YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC); my @files = (); opendir(CURRENT, "$dir"); while (defined(my $file = readdir(CURRENT))) { if ($file =~ /(.*)\.jpg|(.*)\.gif|(.*)\.png/i) { push(@files, "$dir/$file"); } } closedir(CURRENT); my $output_dir = "${dir}_$dir_time_name"; mkdir("$output_dir"); foreach my $jpg(@files) { my $image = Image::Magick->new; $image->Read($jpg); my ($x, $y) = $image->Get('width', 'height'); if ($x >= $y * $WIDTH_PER_HEIGHT) { my $result_ref=Image::Magick::Tiler->new( input_file=>"$jpg", geometry=>'2x1', output_dir=>"$output_dir", output_type=>'jpg', verbose=>0, write=>1, return=>1)->tile(); my @ref = @$result_ref; my @new_files = (); foreach (@ref) { my %obj = %$_; my $created = $obj{'file_name'}; push(@new_files, $created); } if ($READ_DIRECTION) { @new_files = reverse(@new_files); } my @file_path = split(/\//, $jpg); my @file_name = split(/\./, $file_path[1]); my $counter = 1; foreach my $new_file(@new_files) { my $new_image = Image::Magick->new; $new_image->Read("$new_file"); my ($new_x, $new_y) = $new_image->Get('width', 'height'); my $new_magni_x = $RESIZE_X / $new_x; my $new_magni_y = $RESIZE_Y / $new_y; my $new_magnification; if ($new_magni_x < 1 || $new_magni_y < 1) { if ($new_magni_x > $new_magni_y) { $new_magnification = $new_magni_y; } else { $new_magnification = $new_magni_x; } } else { $new_magnification = 1; } $new_image->Resize(width=>$new_x * $new_magnification, height=>$new_y * $new_magnification); binmode(STDOUT); $new_image->Write("$output_dir/$file_name[0]" . '-' . $counter . '.jpg'); unlink("$new_file"); undef $new_image; $counter++; } } else { my $magni_x = $RESIZE_X / $x; my $magni_y = $RESIZE_Y / $y; my $magnification; if ($magni_x < 1 || $magni_y < 1) { if ($magni_x > $magni_y) { $magnification = $magni_y; } else { $magnification = $magni_x; } } else { $magnification = 1; } $image->Resize(width=>$x * $magnification, height=>$y * $magnification); my @file_path = split(/\//, $jpg); my @file_name = split(/\./, $file_path[1]); binmode(STDOUT); $image->Write("$output_dir/$file_name[0]" . '.jpg'); } undef $image; } }