ImageMagickで画像形式変換=スクリプト編=
TEXでepsを使うためにImageMagickで変換しようという話.
ImageMagickでeps形式に画像を変換する - 軸はこーしろーからいきますよ
いちいちコマンド打つのも面倒だったので適当なスクリプトを書いてみた話.
もう半年以上前のこと...
たしかUbuntu12.04とかでやってたはず.
準備
perlで書いてます.
このスクリプトをconvert.plとします.
ディレクトリ構成は
hoge/ +-convert.pl +-source/ | +-picture01.jpg | +-picture02.jpg +-dest/
hogeという作業ディレクトリ直下にconvert.plを置きます.
で,sourceディレクトリに変換前画像をおきます.
実行
そしてconvert.plを実行します.
use strict; use warnings; use File::Basename; use FindBin; my $jpg_dir_name = "jpg"; # sourceディレクトリ名 my $pic_dir_name = "Pict"; # destディレクトリ名 my $this_dir = $FindBin::Bin; my $jpg_dir = "$this_dir/$jpg_dir_name"; my $pic_dir = "$this_dir/$pic_dir_name"; print "##### start converting #####","\n\n"; my $filename = "$jpg_dir/last_run_time.conf"; unless (-f $filename) { system("echo 0 > $filename"); } open( FH , "$filename" ) || die "Error: $filename $!\n"; my $last_run_time = <FH>; $last_run_time = 0 unless ($last_run_time); my $this_time = 0; my $file_exist_flg = 0; opendir DH, $jpg_dir or die "$jpg_dir:$!"; while (my $file = readdir DH) { next if $file =~ /^\.{1,2}.*$/; next if $file =~ /.*\.pl$/; next if $file =~ /.*\.eps$/; next if $file =~ /.*\.conf$/; my $file_full_path = $jpg_dir."/".$file; my $updated_at = (stat $file_full_path)[9]; if ($updated_at > $last_run_time) { if ($this_time < $updated_at) { $this_time = $updated_at; } print "$jpg_dir_name/$file"; my $extlist = ('.jpg'); my $short_name = basename($file, $extlist); system("convert $file_full_path eps2:$pic_dir/$short_name.eps"); print " > $pic_dir_name/$short_name.eps","\n"; $file_exist_flg = 1; } } closedir DH; if ($file_exist_flg) { open(FH, ">$filename"); print FH $this_time; close(FH); print "\n##### complete #####\n"; } else { print "\nThere was no file that can be converted.\n"; }
説明
元ファイルはjpg形式が前提だったので汎用性なんぞ考えていません.
- $jpt_dir_name
- sourceディレクトリ名
- $pic_dir_name
- destディレクトリ名
実行するとsourceディレクトリ内のjpgファイルをdestディレクトリにeps形式に変換して置きます.
sourceディレクトリ内のlast_run_time.confにその実行時に変換したファイルの中で最も更新時間の新しいものを保存します.
次回実行時はlast_run_time.conf内の時間以降に更新されたファイルのみを変換します.
ファイル名が実態通りでないのは仕様を変えたから.
なおすの面倒だったから.
system関数とか使ってるので,なんかね.
で,実行すると
##### start converting ##### jpg/さいてょ01.jpg > Pict/さいてょ01.eps jpg/さいてょ02.jpg > Pict/さいてょ02.eps ##### complete #####
こんなかんじでログを吐きます.
どうなっても知らんよ.