To fix the wrong bounding box information in the eps files, I use the following method
2007/05/13 - [computer/linux] - How to create a stand alone eps file by using pstricks
However, whenever I fix problems, I have to do the same procedure. It was very cumbersome procedures. I decide to make a lazy program, which do the same thing automatically. Here my second python script :
more..fixBBox.py
#!/usr/bin/python
# License : http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
# Module: fixBBox.py
# Author: Jeong Han Lee
# email : citadel.lee@gmail.com
# Date: 2007/11/08
# Version: 0.0.2
#
# 2007.11.07 0.0.1 created
# 2007.11.08 0.0.2
# 1. the boundingbox information correctly
# in the following possible 4 situations:
# @ %%BoundingBox: is or is not
# @ %%HiResBoundingBox: is or is not
# 2. fixed the no response when an empty file is used as an input
import os, sys
import glob
import re
from subprocess import Popen, PIPE
def get_filenames(args):
results = []
for a in args:
#http://docs.python.org/lib/module-glob.html
l = glob.glob(a)
if len(l) > 0:
results = results + l
else:
print 'There is no',a,'file. Please confirm your file name.'
sys.exit()
return results
if len(sys.argv) == 1:
print "----------------------------------------------------------------"
print " fixBBox 0.0.2/2007-11-08 -- fix a bad bounding box in eps files"
print "----------------------------------------------------------------"
print "Usage: fixBBox.py *"
print " : fixBBox.py input.eps"
print " : fixBBox.py input0.eps input2.eps"
print " : fixBBox.py *.eps"
sys.exit()
def info(old, new):
print 'The corrected eps file is ', new
print 'The original eps file is renamed as ', old
def fixbbox(filename, old_filename):
os.rename(filename, old_filename)
info(old_filename, filename)
open_bad_eps = open(old_filename, 'r')
bad_eps = open_bad_eps.read()
gscmd = "gs -q -sDEVICE=bbox -dNOPAUSE -dBATCH - "
# Popen class is defined in the http://docs.python.org/lib/node529.html
# http://docs.python.org/lib/node539.html
gs = Popen(gscmd, stdin=PIPE, stderr=PIPE, shell=True)
(gsin, gsout) = (gs.stdin, gs.stderr)
gsin.write(bad_eps)
gsin.close()
bbox_info = gsout.read()
open_bad_eps.close()
# print bbox_info
(BoundingBox, HiResBoundingBox) = bbox_info.splitlines()
#print 'BBox info ', BoundingBox
#print 'HiBBox info', HiResBoundingBox
if(bad_eps.find('%%BoundingBox:') == -1):
bbox_cond = False
else:
bbox_cond = True
if(bad_eps.find('%%HiResBoundingBox:') == -1):
hibbox_cond = False
else:
hibbox_cond = True
# Regular experssion HOWTO
# http://www.amk.ca/python/howto/regex/regex.html
# http://python.org/doc/lib/node49.html
syntax_only_bbox = '%%BoundingBox: [\d ]+\n'
syntax_only_hibbox = '%%HiResBoundingBox: [\d+(\.\d*) ]+\n'
syntax_bbox = '%%BoundingBox: [\d ]+'
syntax_hibbox = '%%HiResBoundingBox: [\d+(\.\d*) ]+'
pattern_only_bbox = re.compile(syntax_only_bbox)
pattern_only_hibbox = re.compile(syntax_only_hibbox)
pattern_bbox = re.compile(syntax_bbox)
pattern_hibbox = re.compile(syntax_hibbox)
# print 'bad eps :', bad_eps.splitlines()[1:3]
if (bbox_cond):
if(hibbox_cond):
# BBox : TRUE HiResBBox TRUE
temp_eps = re.sub(pattern_bbox, BoundingBox, bad_eps)
# print 'temp', temp_eps.splitlines()[1:3]
fix_eps = re.sub(pattern_hibbox, HiResBoundingBox, temp_eps)
else:
# BBox : TRUE HiResBBox FALSE
fix_eps = re.sub(pattern_only_bbox, bbox_info, bad_eps)
else:
if(hibbox_cond):
# BBox : FALSE HiResBBox TRUE
fix_eps = re.sub(pattern_only_hibbox, bbox_info, bad_eps)
else:
# BBox : FALSE HiResBBox FALSE
syntax_firstline = open(old_filename, 'r').readline()
pattern_firstline = re.compile(syntax_firstline)
fix_eps = re.sub(pattern_firstline, syntax_firstline+bbox_info, bad_eps)
# print 'fix eps :', fix_eps.splitlines()[1:3]
# print fix_eps
open_fix_eps = open(filename,'w')
try:
open_fix_eps.write(fix_eps)
finally:
open_fix_eps.close()
def main():
for filename in get_filenames(sys.argv[1:]):
name, suffix = os.path.splitext(filename)
if suffix == '.eps' :
old_filename = name+"_old" + suffix
fixbbox(filename, old_filename)
else:
print "only eps file can be fixed."
if __name__ == '__main__': main()
The
http://akaihola.blogspot.com/2007/09/goodie-for-your-pdf-toolbox-automatic.html post helps me in order to understand Popen and re. The other references are in the source code.
Note that the above script might delete or destory your eps file. Before using it, please backup your file. 2008.3.28 One guest pointed out a bug in the above script as
Thanks for this great script. But there is a small bug in the regexp.
The Bbox can also contain negative numbers, in which case the compiled
regular expressions won't replace the BBox string.
To fix it replace in the syntax_* variables '[\d ]+' with '[\-\d ]+'
I really appreciate this comment since this is the first and unique feedback. However, the suggestion is not enough to apply the real eps file when I realize ghostscript does not reply the correct bounding box information even if I change the regular expressions. I found the useful note (see page 18) :
http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf.To fix these bug, I must redesign all python script. It also is a time-consuming work for me because I am not a professional programmer. Anyway, this is a sort of TODO list.
If you suffer from the negative bounding box informations, consult the
adobe info booklet or try to understand the following differences. I think you will find the solution.
%%BoundingBox: -85 346 114 529
%%HiResBoundingBox: -85.509269 346.71008 113.18475 528.09641
%%EndComments
%%Page: 1 1
0 560 translate
%%BoundingBox: -85 346 114 529
%%HiResBoundingBox: -85.509269 346.71008 113.18475 528.09641
%%EndComments
%%Page: 1 1
200 560 translate
Then, You will open your eps file, translate, and save it.