I create a large number of images in LaTeX (mostly pstricks
). Some of these images form part of a sequence showing the progression of some algorithm. The progression adds or removes content from the image, which effectively influences the image size, and consequently also the bounding box that surrounds it.
So what I'd like to do is have a script (in any command-line invokable language) that would extract the bounding box components of one file (say FileA.eps
) and replace them with the bounding box components of another file (say FileB.eps
). Sometimes I want to do this for only the y
-components, sometimes only for the x
-components, sometimes for only a single component (it depends on the sequence showing the progression). For example, consider the following two files created using a latex
->dvips
sequence:
FileA.eps
%!PS-Adobe-2.0 EPSF-2.0 %%BoundingBox: 170 378 252 452 %%HiResBoundingBox: 170.340 378.474 251.880 451.626 %%Creator: dvips(k) 5.992 Copyright 2012 Radical Eye Software ...
FileB.eps
%!PS-Adobe-2.0 EPSF-2.0 %%BoundingBox: 148 365 269 478 %%HiResBoundingBox: 148.446 365.940 268.483 477.651 %%Creator: dvips(k) 5.992 Copyright 2012 Radical Eye Software ...
I'd like to have FileA.eps
to be updated to
FileA.eps
%!PS-Adobe-2.0 EPSF-2.0 %%BoundingBox: 170 365 252 478 %%HiResBoundingBox: 170.340 365.940 251.880 477.651 %%Creator: dvips(k) 5.992 Copyright 2012 Radical Eye Software ...
where the y
-coordinates of FileB.eps
was used to replace the y
-coordinates in the original FileA.eps
. Note that this change holds for both %%BoundingBox
and %%HiResBoundingBox
.
Ideally I'd like some generic script boundingboxscript
that is invoked using
[lang] boundinboxscript FileA.eps FileB.eps
where [lang]
is the language (like perl
or ruby
) and FileA.eps
is edited in-place. This discussion originated from the TeX, LaTeX & Friends chat room. I'm running Windows 7.
I know nothing about perl, but maybe you can convert this ruby script:
outfile = ARGV[0]
infile = ARGV[1]
opts = ARGV[2]
unless File.read(infile) =~ /%%BoundingBox: (\d+) (\d+) (\d+) (\d+)/
puts "Invalid input file."
exit!
else
x, y, w, h = $1, $2, $3, $4
eps = File.read(outfile).sub(/%%BoundingBox: (\d+) (\d+) (\d+) (\d+)/) do
x = $1 unless opts.include? "x"
y = $2 unless opts.include? "y"
w = $3 unless opts.include? "w"
h = $4 unless opts.include? "h"
"%%BoundingBox: #{x} #{y} #{w} #{h}"
end
File.write(outfile, eps)
end
Invoke with:
ruby boundinboxscript.rb FileA.eps FileB.eps xywh
The last option is what you want to take from FileB.eps
and put in FileA.eps
.