fix parser breakage
[PyX/mjg.git] / contrib / epstopng.py
blobf61bad1607fb60d3176fda5066c54e0a895556fc
1 #!/usr/bin/env python
4 # Copyright (C) 2003 André Wobst <wobsta@users.sourceforge.net>
6 # epstopng is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # epstopng is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with epstopng; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # TODO: - set dpi in png (don't know how to do this in PIL)
21 # - this is much too slow --- consider a rewrite in C
24 import getopt, sys, os, StringIO, re, math
25 import Image # we use the python image library (PIL)
28 progname = "epstopng v0.1: eps to transparent antialiased png converter"
31 def epstopng(epsname, pngname, resolution, scale, transparent, gsname, quiet):
32 sys.stderr.write("run ghostscript to create a %i times larger non-antialiased image ... " % scale)
33 gsout = os.popen("%s -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -sDEVICE=ppmraw -sOutputFile=- -r%i %s" % (gsname, resolution*scale, epsname))
34 input = Image.open(StringIO.StringIO(gsout.read())).convert("RGB") # ensure rgb here
35 output = Image.new("RGBA", [(x+scale-1)/scale for x in input.size])
36 sys.stderr.write("done\n")
37 sys.stderr.write("image size is %ix%i\n" % output.size)
39 inputdata = input.getdata() # getdata instead of getpixel leads to about a factor of 2 in running time
40 # similar effect with putdata possible??? (didn't got that running)
41 # loop over the small image
42 for x in range(output.size[0]):
43 sys.stderr.write("transparent antialias conversion ... %5.2f %%\r" % (x*100.0/output.size[0]))
44 for y in range(output.size[1]):
45 # r, g, b: sum of the rgb-values; u: number of non-transparent pixels
46 r = g = b = u = 0
47 # loop over the pixels of the large image to be used for pixel (x, y)
48 for xo in range(x*scale, (x+1)*scale):
49 for yo in range(y*scale, (y+1)*scale):
50 if xo < input.size[0] and yo < input.size[1]:
51 ro, go, bo = inputdata[xo+input.size[0]*yo]
52 if (ro, go, bo) != transparent:
53 r += ro
54 g += go
55 b += bo
56 u += 1
57 if u:
58 output.putpixel((x, y), (r/u, g/u, b/u, (u*255)/(scale*scale)))
59 else:
60 output.putpixel((x, y), (255, 255, 255, 0))
61 sys.stderr.write("transparent antialias conversion ... done \n")
63 output.save(pngname)
66 def usage():
67 print progname
68 print "Copyright (C) 2003 André Wobst <wobsta@users.sourceforge.net>"
69 print "usage: epstopng [options] <eps-file>"
70 print "-h, --help: show this help"
71 print "-q, --quiet: be quiet"
72 print "-o, --output <file>: output file name; must be set"
73 print "-r, --resolution <dpi>: resolution; default: 100"
74 print "-s, --scale <scale>: input scale for antialias; default: 4"
75 print "-t, --transparent (<r>, <g>, <b>): transparent color; default: (255, 255, 255)"
76 print "-g, --gsname <name>: name of the gs interpreter (gs version >= 8.0 needed!); default: \"gs\""
79 def main():
80 try:
81 opts, args = getopt.getopt(sys.argv[1:], "hqo:r:s:t:g:", ["help", "quiet", "output", "resolution", "scale", "transparent", "gsname"])
82 except getopt.GetoptError:
83 # print help information and exit:
84 usage()
85 sys.exit(2)
86 output = None
87 resolution = 100
88 scale = 4
89 transparent = (255, 255, 255)
90 gsname = "gs"
91 quiet = 0
92 for o, a in opts:
93 if o in ("-h", "--help"):
94 usage()
95 sys.exit()
96 if o in ("-o", "--output"):
97 output = a
98 if o in ("-r", "--resolution"):
99 resolution = int(a)
100 if o in ("-s", "--scale"):
101 scale = int(a)
102 if o in ("-t", "--transparent"):
103 m = re.compile(r"\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$").match(a)
104 if not m:
105 raise RuntimeError("transparent argument does not match")
106 transparent = [int(x) for x in m.groups()]
107 if o in ("-g", "--gsname"):
108 gsname = a
109 if len(args) == 1:
110 input = args[0]
111 elif len(args):
112 raise RuntimeError("can't handle several input files")
113 else:
114 raise RuntimeError("must specify an input file (reading from stdin is not yet supported)")
115 if output is None:
116 raise RuntimeError("you must specify an output file name")
117 if not quiet:
118 sys.stderr.write(progname + "\n")
119 epstopng(input, output, resolution, scale, transparent, gsname, quiet)
122 if __name__ == "__main__":
123 main()