From f0d6776ad932f2c2352242ecde427d674799ae1f Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Mon, 27 Sep 2010 21:51:24 +1000 Subject: [PATCH 1/6] Add JPEG output support. --- gmap.py | 14 +++++++++++--- quadtree.py | 48 +++++++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/gmap.py b/gmap.py index 97eeb97..00c7836 100755 --- a/gmap.py +++ b/gmap.py @@ -32,8 +32,7 @@ import quadtree helptext = """ %prog [OPTIONS] -%prog -d [tiles dest dir] -""" +%prog -d [tiles dest dir]""" def main(): try: @@ -46,6 +45,7 @@ def main(): parser.add_option("-d", "--delete", dest="delete", help="Clear all caches. Next time you render your world, it will have to start completely over again. This is probably not a good idea for large worlds. Use this if you change texture packs and want to re-render everything.", action="store_true") parser.add_option("--cachedir", dest="cachedir", help="Sets the directory where the Overviewer will save chunk images, which is an intermediate step before the tiles are generated. You must use the same directory each time to gain any benefit from the cache. If not set, this defaults to your world directory.") parser.add_option("--chunklist", dest="chunklist", help="A file containing, on each line, a path to a chunkfile to update. Instead of scanning the world directory for chunks, it will just use this list. Normal caching rules still apply.") + parser.add_option("--imgformat", dest="imgformat", help="The image output format to use. Currently supported: png(default), jpg. NOTE: png will always be used as the intermediate image format.") options, args = parser.parse_args() @@ -85,12 +85,20 @@ def main(): else: chunklist = None + if options.imgformat: + if options.imgformat not in ('jpg','png'): + parser.error("Unknown imgformat!") + else: + imgformat = options.imgformat + else: + imgformat = 'png' + # First generate the world's chunk images w = world.WorldRenderer(worlddir, cachedir, chunklist=chunklist) w.go(options.procs) # Now generate the tiles - q = quadtree.QuadtreeGen(w, destdir, depth=options.zoom) + q = quadtree.QuadtreeGen(w, destdir, depth=options.zoom, imgformat=imgformat) q.go(options.procs) def delete_all(worlddir, tiledir): diff --git a/quadtree.py b/quadtree.py index a075e77..fc65f2d 100644 --- a/quadtree.py +++ b/quadtree.py @@ -52,7 +52,7 @@ def catch_keyboardinterrupt(func): return newfunc class QuadtreeGen(object): - def __init__(self, worldobj, destdir, depth=None): + def __init__(self, worldobj, destdir, depth=None, imgformat=None): """Generates a quadtree from the world given into the given dest directory @@ -62,6 +62,9 @@ class QuadtreeGen(object): minimum depth that contains all chunks is calculated and used. """ + assert(imgformat) + self.imgformat = imgformat + if depth is None: # Determine quadtree depth (midpoint is always 0,0) for p in xrange(15): @@ -159,8 +162,8 @@ class QuadtreeGen(object): newdir = "new" + str(dirnum) newdirpath = getpath(newdir) - files = [str(dirnum)+".png", str(dirnum)+".hash", str(dirnum)] - newfiles = [str(newnum)+".png", str(newnum)+".hash", str(newnum)] + files = [str(dirnum)+"."+imgformat, str(dirnum)+".hash", str(dirnum)] + newfiles = [str(newnum)+"."+imgformat, str(newnum)+".hash", str(newnum)] os.mkdir(newdirpath) for f, newf in zip(files, newfiles): @@ -221,7 +224,7 @@ class QuadtreeGen(object): # (even if tilechunks is empty, render_worldtile will delete # existing images if appropriate) yield pool.apply_async(func=render_worldtile, args= (tilechunks, - colstart, colend, rowstart, rowend, dest)) + colstart, colend, rowstart, rowend, dest, self.imgformat)) def _apply_render_inntertile(self, pool, zoom): """Same as _apply_render_worltiles but for the inntertile routine. @@ -233,7 +236,7 @@ class QuadtreeGen(object): dest = os.path.join(self.destdir, "tiles", *(str(x) for x in path[:-1])) name = str(path[-1]) - yield pool.apply_async(func=render_innertile, args= (dest, name)) + yield pool.apply_async(func=render_innertile, args= (dest, name, self.imgformat)) def go(self, procs): """Renders all tiles""" @@ -317,7 +320,7 @@ class QuadtreeGen(object): pool.join() # Do the final one right here: - render_innertile(os.path.join(self.destdir, "tiles"), "base") + render_innertile(os.path.join(self.destdir, "tiles"), "base", self.imgformat) def _get_range_by_path(self, path): """Returns the x, y chunk coordinates of this tile""" @@ -348,28 +351,28 @@ class QuadtreeGen(object): return chunklist @catch_keyboardinterrupt -def render_innertile(dest, name): +def render_innertile(dest, name, imgformat): """ - Renders a tile at os.path.join(dest, name)+".png" by taking tiles from + Renders a tile at os.path.join(dest, name)+".ext" by taking tiles from os.path.join(dest, name, "{0,1,2,3}.png") """ - imgpath = os.path.join(dest, name) + ".png" + imgpath = os.path.join(dest, name) + "." + imgformat hashpath = os.path.join(dest, name) + ".hash" if name == "base": - q0path = os.path.join(dest, "0.png") - q1path = os.path.join(dest, "1.png") - q2path = os.path.join(dest, "2.png") - q3path = os.path.join(dest, "3.png") + q0path = os.path.join(dest, "0." + imgformat) + q1path = os.path.join(dest, "1." + imgformat) + q2path = os.path.join(dest, "2." + imgformat) + q3path = os.path.join(dest, "3." + imgformat) q0hash = os.path.join(dest, "0.hash") q1hash = os.path.join(dest, "1.hash") q2hash = os.path.join(dest, "2.hash") q3hash = os.path.join(dest, "3.hash") else: - q0path = os.path.join(dest, name, "0.png") - q1path = os.path.join(dest, name, "1.png") - q2path = os.path.join(dest, name, "2.png") - q3path = os.path.join(dest, name, "3.png") + q0path = os.path.join(dest, name, "0." + imgformat) + q1path = os.path.join(dest, name, "1." + imgformat) + q2path = os.path.join(dest, name, "2." + imgformat) + q3path = os.path.join(dest, name, "3." + imgformat) q0hash = os.path.join(dest, name, "0.hash") q1hash = os.path.join(dest, name, "1.hash") q2hash = os.path.join(dest, name, "2.hash") @@ -434,13 +437,16 @@ def render_innertile(dest, name): img.paste(quad3, (192, 192)) # Save it - img.save(imgpath) + if imgformat == 'jpg': + img.save(imgpath, quality=95, optimize=True) + else: # png + img.save(imgpath) with open(hashpath, "wb") as hashout: hashout.write(newhash) @catch_keyboardinterrupt -def render_worldtile(chunks, colstart, colend, rowstart, rowend, path): +def render_worldtile(chunks, colstart, colend, rowstart, rowend, path, imgformat): """Renders just the specified chunks into a tile and save it. Unlike usual python conventions, rowend and colend are inclusive. Additionally, the chunks around the edges are half-way cut off (so that neighboring tiles @@ -449,7 +455,7 @@ def render_worldtile(chunks, colstart, colend, rowstart, rowend, path): chunks is a list of (col, row, filename) of chunk images that are relevant to this call - The image is saved to path+".png" and a hash is saved to path+".hash" + The image is saved to path+".ext" and a hash is saved to path+".hash" If there are no chunks, this tile is not saved (if it already exists, it is deleted) @@ -490,7 +496,7 @@ def render_worldtile(chunks, colstart, colend, rowstart, rowend, path): # Before we render any tiles, check the hash of each image in this tile to # see if it's changed. hashpath = path + ".hash" - imgpath = path + ".png" + imgpath = path + "." + imgformat if not chunks: # No chunks were found in this tile From 244679a8772a180539b320dcff270b497cb4be21 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Mon, 27 Sep 2010 22:09:58 +1000 Subject: [PATCH 2/6] Update README with --imgformat details. --- README.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.rst b/README.rst index 007de92..be5e232 100644 --- a/README.rst +++ b/README.rst @@ -121,6 +121,12 @@ Options python gmap.py --cachedir= +--imgformat=FORMAT + Set the output image format used for the tiles. The default is 'png', + but 'jpg' is also supported. Note that regardless of what you choose, + Overviewer will still use PNG for cached images to avoid recompression + artifacts. + -p PROCS, --processes=PROCS Adding the "-p" option will utilize more cores during processing. This can speed up rendering quite a bit. The default is set to the same From e6bfcc30333bc394affb1eba383e7af1745475d0 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Mon, 27 Sep 2010 22:10:23 +1000 Subject: [PATCH 3/6] Write HTML file with appropriate extension config. --- quadtree.py | 6 ++++-- template.html | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/quadtree.py b/quadtree.py index fc65f2d..36d6818 100644 --- a/quadtree.py +++ b/quadtree.py @@ -112,13 +112,15 @@ class QuadtreeGen(object): print "{0}/{1} tiles complete on level {2}/{3}".format( complete, total, level, self.p) - def write_html(self, zoomlevel): + def write_html(self, zoomlevel, imgformat): """Writes out index.html""" templatepath = os.path.join(os.path.split(__file__)[0], "template.html") html = open(templatepath, 'r').read() html = html.replace( "{maxzoom}", str(zoomlevel)) + html = html.replace( + "{imgformat}", str(imgformat)) with open(os.path.join(self.destdir, "index.html"), 'w') as output: output.write(html) @@ -263,7 +265,7 @@ class QuadtreeGen(object): else: pool = multiprocessing.Pool(processes=procs) - self.write_html(self.p) + self.write_html(self.p, self.imgformat) # Render the highest level of tiles from the chunks results = collections.deque() diff --git a/template.html b/template.html index ef2868f..e6105c1 100644 --- a/template.html +++ b/template.html @@ -14,7 +14,7 @@