# Python script for simplifying EPS files generated by Adobe Illustrator files. # Based on sample files from Adobe Illustrator 4.0 for Windows. # Works by removing unused patterns from %%BeginSetup ... %%EndSetup # # Important Note: This script is designed to be used with # EPS files saved in *Illustrator 4* format. It will not work # on files in Illustrator 10's default format. It does provide # some improvement on files up to at least Illustrator 7, but # you're best off with saving as Illustrator 4. # # Known problems: While Mac OS X is Unix, if you # use a Unix-type python installation, then you need to change the # line-breaks in the EPS file to Unix, not Mac, format (e.g. with # perl or BBedit). # # To run under Unix, place this file in the directory that contains the files you # want to reduce and do "python fix_ill_ps.py." # # Under Windows or MacOS 9, place in the directory in question and double-click # this file. # # In all cases this script tries to simplify all files ending in ".eps". The original files # are moved to ".eps.old". # # By Nathan Dunfield # # Version of 2002/11/23 import os, sys, re, glob def patterns_used(file_name): pats_avail = [] pats_used = [] watch = 0 for line in open(file_name).xreadlines(): s = re.match("%AI.*_BeginPattern: \((.*)\)", line) if s: pats_avail.append(s.group(1)) s = re.match("%%EndSetup", line) if s: watch = 1 if watch: for pat in pats_avail: if line.find(pat) != -1: if not pat in pats_used: pats_used.append(pat) return pats_used def fix_ps(file_name): # find those patterns the file actually uses pats_used = patterns_used(file_name) # copy the file to ".old" f = open(file_name + ".old","w") for line in open(file_name).xreadlines(): f.write(line) f.close() # create new file, stripping out those patterns that # are not used. printing = 1 f = open(file_name, "w") for line in open(file_name + ".old").xreadlines(): s = re.match("%AI.*_BeginPattern: \((.*)\)", line) if s: if not s.group(1) in pats_used: printing = 0 s = re.match("%AI.*_EndPattern\n", line) if s: printing = 1 continue if printing: f.write(line) f.close() return (os.stat(file_name + ".old").st_size/1024, os.stat(file_name).st_size/1024) def main(): print "Reducing size of all .eps files in this directory that" print "were generated by Adobe Illustrator; this is done " print "by removing unused patterns.\n" print "The original files are moved from .eps to .eps.old" print "\n" total_old, total_new = 0, 0 for file_name in glob.glob("*.eps"): old, new = fix_ps(file_name) print file_name, " %dK --> %dK" % (old, new) total_old += old total_new += new if total_old == 0: print "No files to process\n" else: print "\nTotal reduction was %dK --> %dK " % (total_old, total_new) print "Final files are %.1f" % ((total_new*100.0)/total_old) + "% of their original size." print "\nHit return to finish.\n" inp = sys.stdin.readline() main()