(Application) PSファイルをPNGファイルに変換
作成者: 西本 絵梨子
概要
- PostScriptファイルから適宜余白を取り除いてPNGファイルに変換します.
eps2png が必要です(最新版は2.7(2011-03-14現在)).
$ wget http://search.cpan.org/CPAN/authors/id/J/JV/JV/eps2png-2.7.tar.gz $ tar xvzf eps2png-2.7.tar.gz $ perl Makefile.PL $ make all $ make test エラーが出ても気にしない! (たぶん…) $ sudo make install
- DCL-X.X パッケージに含まれるコマンド(dclpsrot, dclpsrmcm, dclpsmargin)を組み合わせています.
使い方
% ./pstopng.rb test.ps ==> test.png が吐き出されます
私は % chmod a+x pstopng.rb をしてパスの通っているところに置いています.
DESCRIPTION
% pstopng.rb --help
Usage: pstopng.rb -r, --resolution [value] specify a png resolution ( defalut 164 ) -o, --output [string] specify a output png file -odir, --outdir [string] specify a output directory --norotate without rotating ( defalut rotate ) -h, --help show this message
ソースコード
#! /usr/bin/ruby
#
require 'getoptlong.rb'
parser = GetoptLong.new
parser.set_options(
['--resolution','--r', GetoptLong::REQUIRED_ARGUMENT],
['--output','--o', GetoptLong::REQUIRED_ARGUMENT],
['--outdir','--odir', GetoptLong::REQUIRED_ARGUMENT],
['--norotate', GetoptLong::NO_ARGUMENT],
['--margin', '--m', GetoptLong::REQUIRED_ARGUMENT],
['--help', '-h', GetoptLong::NO_ARGUMENT]
)
#-- initial values
resl = 164 # defalut resolution
outfname = false
outdir = false
norotate = false
margin = 0.05
help = false
#-- get input values
parser.each_option do |name,arg|
name.sub!(/^--/,'')
case name
when 'resolution' , 'r'
resl = arg.to_i
when 'output' , 'o'
outfname = arg.to_s
when 'outdir' , 'odir'
outdir = arg.to_s
when 'norotate'
norotate = true
when 'margin' , 'm'
margin = arg.to_f
when 'help' , 'h'
help = true
end
end
#-- help message
if (help)
usage = "Usage: #{$0.split('/')[-1]}
-r, --resolution [value] specify a png resolution ( defalut #{resl} )
-o, --output [string] specify a output png file
-odir, --outdir [string] specify a output directory
--norotate without rotating ( defalut rotate )
-h, --help show this message"
puts usage
exit
end
#-- make ps file to png
home = Dir.getwd
ARGV.each{|input|
ok = File.exist?(input)
if (not ok)
puts "Not exist #{input} !!\n\n"
else
dirname = File.dirname(input)
psfname = File.basename(input)
outfname = File.basename(input,'.ps')
puts "#{outfname}"
Dir.chdir(dirname)
puts "PS -> EPS ..."
if (norotate)
system("dclpsrmcm #{psfname} | dclpsmargin -m #{margin} > #{outfname}.eps")
else
system("dclpsrot #{psfname} | dclpsrmcm | dclpsmargin -m #{margin} > #{outfname}.eps")
end
puts "EPS -> PNG ..."
system("eps2png -resolution #{resl} #{outfname}.eps")
puts "clean up ...\n\n"
system("rm -f #{outfname}.eps")
Dir.chdir(home)
if (outdir)
puts "move to #{outdir}\n"
system("mv #{dirname}/#{outfname}.png #{outdir}")
end
end
}
謝辞
このスクリプトは, 神代さんに教えていただいたものを改変したものです.
キーワード:[Postscript] [PNG]
参照: