#!/usr/local/bin/perl # Resize jpeg files # Last modified Sun May 21 18:43:05 2000 on firestorm # 基本的な使い方 # あるディレクトリにたくさんの大きなJPEGファイル(デジカメで撮ったままの # ファイルとか)があるときに、それらをカレントディレクトリに適当なサイズ/ # 明るさに一括変換して吐き出す。吐き出したjpgファイルのコメントエリアに # はそのファイルのタイムスタンプが埋め込まれる。 # # 【WWW用に幅400くらいにしたい】 # % rjpg -x 400 /dos/d/*jpg # 【やっぱりもとの半分のサイズで】 # % rjpg -s 0.5 -f /dos/d/*jpg (-f付けないと上書きしない) # 【γ値1.5で】 # % rjpg -x 400 -g 1.5 /dos/d/*.jpg (デフォルトは1.2) # 【90度回転して】 # % rjpg -x 400 -r 90 /dos/d/dcp01234.jpg # 【中央部70%だけ残るようトリミング】 # % rjpg -x 600 -c 70% /dos/d/dcp01235.jpg $rdjpg = "rdjpgcom"; $wrjpg = "wrjpgcom"; $djpg = "djpeg"; $cjpg = "cjpeg"; $pnmgamma = "pnmgamma"; $pnmcut = "pnmcut"; $cat = "cat"; $outputdir = "."; $resize = 0; $quality = "75"; $gamma = "1.2"; $debug = 0; $scale = 1; $width = 0; $force = 0; $rotate = 0; $fntitle = 0; $usage = <<_EOU_; $0 [options] JpegFiles Options are... -x WIDTH Set new jpg file's width to WIDTH -s SCALE Set scale (for pnmscale) to SCALE -o DIR Output directory -q N Jpeg compression quality -f Force Overwrite -g N Gamma correction -r ANGLE Rotate in unti-clockwise -c X,Y,W,H Cut W*H rectangle from coordinate (X,Y) -c N% Cut the N% center of photo -p Create progressive JPG -fn Put file name in jpg comment area, instead of time _EOU_ while ($_ = $ARGV[0], /^-.+/ && shift) { last if /^--$/; while (/^-[A-z]/) { if (/^-x$/) { $width = shift; } elsif (/^-s$/) { $scale = shift; } elsif (/^-d$/) { $debug++; } elsif (/^-o$/) { $outputdir = shift; } elsif (/^-q$/) { $quality = shift; } elsif (/^-f$/) { $force++; } elsif (/^-g/) { $gamma = shift; } elsif (/^-r/) { $rotate = shift; } elsif (/^-p$/) { $cjpg .= " -progressive"; } elsif (/^-fn$/) { $fntitle++; s/fn//; } elsif (/^-c$/) { $_ = shift; if (/(\d+)%/) { $cut = $1; } else { $cut=$_; ($cutx, $cuty) = (split(/,/, $cut))[2,3]; $cut = "$pnmcut " . join(" ", split(/,/, $_)); } } else { print "Invalid option $_\n"; print $usage; exit 0; } s/^-.(.*)/-$1/; } } foreach $f (@ARGV) { if (! -f $f) { print STDERR "No such file: $f\n"; next; } $timestamp = (stat($f))[9]; chop($comment = `$rdjpg $f`); if ($comment) { $stamp = $comment; print "$f Comment: $stamp\n" if $debug; } elsif (!$fntitle) { $stamp = &timeformat($timestamp); print "$f Stamp: $stamp\n" if $debug; } else { $stamp = &basename($f); } if ($width) { local($x, $y) = (`$djpg $f|pnmfile -` =~ /(\d+) by (\d+)/); if ($width == $x) { $scale = 1; } else { if ($cutx && $cuty) { $height = $width*$cuty/$cutx; } else { $height = $y*$width/$x; } $resize = "pnmscale -xsize $width -ysize $height"; } } elsif ($scale) { $resize = "pnmscale $scale"; } $SIG{'INT'} = 'break'; $outfile = "$outputdir/" . &basename($f); # sleep(1); if (-f $outfile) { local($dev1, $ino1) = (stat($f))[0,1]; local($dev2, $ino2) = (stat($outfile))[0,1]; if ($dev1==$dev2 && $ino1==$ino2) { print "Skipping $f...\n"; next; } if (!$force) { $outfile =~ s,\.jpg$,\.jpeg,; print "Set out file to $outfile\n"; } } open(IN, $f); binmode(IN); if ($resize) { if ("$cut" =~ /$pnmcut/) { $cmdline = "$djpg | $cut"; } elsif ($cut > 0) { local($ox, $oy) = (`$rdjpg -v $f` =~ /(\d+)w \* (\d+)h/); local($x1, $y1, $w1, $h1) = ($ox/2*(100-$cut)/100, $oy/2*(100-$cut)/100, $ox*$cut/100, $oy*$cut/100); $cmdline = "$djpg | $pnmcut $x1 $y1 $w1 $h1"; } else { $cmdline = "$djpg"; } $cmdline .= " | $resize | $pnmgamma $gamma "; if ($rotate) { $cmdline .= "| pnmrotate $rotate"; } $cmdline .= "| $cjpg -q $quality | $wrjpg -c '$stamp' > $outfile"; print "cat $f | $cmdline\n"; open(OUT, "| $cmdline"); binmode(OUT); print OUT while(); close(OUT); } else { # resizing print("$wrjpg -c '$stamp' $f > $outfile\n") unless $quiet; # system("$wrjpg -c '$stamp' $f > $outfile"); open(OUT, "| $wrjpg -c '$stamp' > $outfile"); binmode(OUT); print OUT ; close(OUT); } close(IN); $SIG{'INT'} = DEFAULT; utime($timestamp, $timestamp, $outfile); } # #!/usr/local/bin/perl # print &timeformat((stat("$file"))[9]), "\n"; sub break { unlink($outfile) if (-f $outfile); die"User break: $outfile deleted\n"; exit 0; } #----------------------------------------# # 作る日付 #----------------------------------------# sub timeformat { local($time) = @_; local($thisday, $thismon, $date); local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time); $thisday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[($wday)]; $thismon = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[($mon)]; # $date = sprintf("%s %s %02d %02d:%02d:%02d JST %d" $date = sprintf("%s %s %02d %02d:%02d:%02d %d" ,$thisday,$thismon,$mday,$hour,$min,$sec,1900+$year); $date; } sub basename { local($path) = @_; local($dir, $file) = ($path =~ m,(.*)/(.*),); if ($file) { return $file; } else { return $path; } }