From 6a4dea33d4b49206141ea309a32f5a0ef75379ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Wobst?= Date: Mon, 31 Mar 2003 10:15:47 +0000 Subject: [PATCH] epstopng git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@843 069f4177-920e-0410-937b-c2a4a81bcd90 --- contrib/epstopng.py | 128 +++++++++++++++++++++++++++++++++++++++++ examples/Makefile | 5 +- examples/graphs/partialfill.py | 2 +- www/Makefile | 5 +- www/png/Makefile | 6 +- 5 files changed, 133 insertions(+), 13 deletions(-) create mode 100644 contrib/epstopng.py diff --git a/contrib/epstopng.py b/contrib/epstopng.py new file mode 100644 index 00000000..ca576a70 --- /dev/null +++ b/contrib/epstopng.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python +# +# +# Copyright (C) 2003 André Wobst +# +# epstopng is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# epstopng is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with epstopng; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# TODO: - set dpi in png (don't know how to do this in PIL) +# - this is much too slow --- consider a rewrite in C + + +import getopt, sys, os, StringIO, re +import Image # we use the python image library (PIL) + + +progname = "epstopng v0.1: eps to transparent antialiased png converter" + + +def epstopng(epsname, pngname, resolution, scale, transparent, gsname, quiet): + sys.stderr.write("run ghostscript to create a %i times larger non-antialiased image ... " % scale) + gsout = os.popen("%s -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -sDEVICE=ppmraw -sOutputFile=- -r%i %s" % (gsname, resolution*scale, epsname)) + input = Image.open(StringIO.StringIO(gsout.read())).convert("RGB") # ensure rgb here + output = Image.new("RGBA", [(x+scale-1)/scale for x in input.size]) + sys.stderr.write("done\n") + sys.stderr.write("image size is %ix%i\n" % output.size) + + inputdata = input.getdata() # getdata instead of getpixel leads to about a factor of 2 in running time + # similar effect with putdata possible??? (didn't got that running) + # loop over the small image + for x in range(output.size[0]): + sys.stderr.write("transparent antialias conversion ... %5.2f %%\r" % (x*100.0/output.size[0])) + for y in range(output.size[1]): + # r, g, b: sum of the rgb-values; u: number of non-transparent pixels + r = g = b = u = 0 + # loop over the pixels of the large image to be used for pixel (x, y) + for xo in range(x*scale, (x+1)*scale): + for yo in range(y*scale, (y+1)*scale): + if xo < input.size[0] and yo < input.size[1]: + ro, go, bo = inputdata[xo+input.size[0]*yo] + if (ro, go, bo) != transparent: + r += ro + g += go + b += bo + u += 1 + if u: + output.putpixel((x, y), (r/u, g/u, b/u, (u*255)/(scale*scale))) + else: + output.putpixel((x, y), (255, 255, 255, 0)) + sys.stderr.write("transparent antialias conversion ... done \n") + + output.save(pngname) + + +def usage(): + print progname + print "Copyright (C) 2003 André Wobst " + print "usage: epstopng [options] " + print "-h, --help: show this help" + print "-q, --quiet: be quiet" + print "-o, --output : output file name; must be set" + print "-r, --resolution : resolution; default: 100" + print "-s, --scale : input scale for antialias; default: 4" + print "-t, --transparent (, , ): transparent color; default: (255, 255, 255)" + print "-g, --gsname : name of the gs interpreter (gs version >= 8.0 needed!); default: \"gs\"" + + +def main(): + try: + opts, args = getopt.getopt(sys.argv[1:], "hqo:r:s:t:g:", ["help", "quiet", "output", "resolution", "scale", "transparent", "gsname"]) + except getopt.GetoptError: + # print help information and exit: + usage() + sys.exit(2) + output = None + resolution = 100 + scale = 4 + transparent = (255, 255, 255) + gsname = "gs" + quiet = 0 + for o, a in opts: + if o in ("-h", "--help"): + usage() + sys.exit() + if o in ("-o", "--output"): + output = a + if o in ("-r", "--resolution"): + if math.ceil(a) != math.floor(a): + raise RuntimeError("integer resolution expected") + resolution = a + if o in ("-s", "--scale"): + if math.ceil(a) != math.floor(a): + raise RuntimeError("integer scale expected") + scale = int(a) + if o in ("-t", "--transparent"): + m = re.compile(r"\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d)\s*\)\s*$").match(a) + if not m: + raise RuntimeError("transparent argument does not match") + transparent = [int(x) for x in m.groups()] + if o in ("-g", "--gsname"): + gsname = a + if len(args) == 1: + input = args[0] + elif len(args): + raise RuntimeError("can't handle several input files") + else: + raise RuntimeError("must specify an input file (reading from stdin is not yet supported)") + if output is None: + raise RuntimeError("you must specify an output file name") + if not quiet: + sys.stderr.write(progname + "\n") + epstopng(input, output, resolution, scale, transparent, gsname, quiet) + + +if __name__ == "__main__": + main() + diff --git a/examples/Makefile b/examples/Makefile index f8b9905f..769ea835 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -40,7 +40,4 @@ html: $(htmlfiles) $(pngfiles) %.png: %.eps @# -dEPSCrop needs gs version >= 8.0 - gs8.0 -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -sDEVICE=ppmraw -sOutputFile=- -r400 $*.eps > $@.large - ppmcolormask white $@.large|pnmscale 0.25 2> /dev/null > $@.alpha - pnmscale 0.25 $@.large|pnmtopng -alpha $@.alpha > $@ - rm $@.large $@.alpha + python ../contrib/epstopng.py -g gs8.0 -o $@ $^ diff --git a/examples/graphs/partialfill.py b/examples/graphs/partialfill.py index 3503d0bb..b4af5044 100644 --- a/examples/graphs/partialfill.py +++ b/examples/graphs/partialfill.py @@ -16,7 +16,7 @@ horiz = path.normpath(horiz.style.path) # intersect the lines splith, splitf = horiz.intersect(fline) -# create gray area +# create gray area (we do not use simple clipping) area = horiz.split(splith[0])[0] for i in range(0, len(splith)-2, 2): area = area.glue(fline.split(splitf[i], splitf[i+1])[1]) diff --git a/www/Makefile b/www/Makefile index 7513c78c..157e0578 100644 --- a/www/Makefile +++ b/www/Makefile @@ -14,6 +14,7 @@ cssfiles = main.css builddir = build html: $(htmlfiles) $(pngfiles) + cd png; make install: html -mkdir $(builddir) @@ -27,7 +28,3 @@ install: html %.html: %.pt maintemplate.pt pt2html.py PYTHONPATH=/usr/src/ZopeCVS/Zope3/src ./pt2html.py $< - -%.eps.png: %.eps - @# -dEPSCrop needs gs version >= 8.0 - gs8.0 -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -sDEVICE=ppmraw -sOutputFile=- -r400 $^|pnmscale 0.25|pnmtopng > $^.png diff --git a/www/png/Makefile b/www/png/Makefile index d274a97b..027d1294 100644 --- a/www/png/Makefile +++ b/www/png/Makefile @@ -10,7 +10,5 @@ clean: %.png: %.py export PYTHONPATH=$(PWD)/../.. ; cd $(dir $^) ; python $(notdir $^) @# -dEPSCrop needs gs version >= 8.0 - gs8.0 -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -sDEVICE=ppmraw -sOutputFile=- -r400 $*.eps > $@.large - ppmcolormask white $@.large|pnmscale 0.25 2> /dev/null > $@.alpha - pnmscale 0.25 $@.large|pnmtopng -alpha $@.alpha > $@ - rm $@.large $@.alpha $*.eps + python ../../contrib/epstopng.py -g gs8.0 -o $@ $*.eps + rm $*.eps -- 2.11.4.GIT