Add --optimize-img={1,2} -- performs postprocessing on images
This adds basic post-processing to images. At the moment, it only performs actions for the png output type, but changes to work for jpeg will be exceptionall minimal.
This commit is contained in:
8
gmap.py
8
gmap.py
@@ -49,6 +49,7 @@ def main():
|
||||
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.")
|
||||
parser.add_option("--optimize-img", dest="optimizeimg", help="If using png, perform image file size optimizations on the output. Specify 1 for pngcrush, 2 for pngcrush+optipng+advdef. This may double (or more) render times, but will produce up to 30% smaller images. NOTE: requires corresponding programs in $PATH or %PATH%")
|
||||
parser.add_option("-q", "--quiet", dest="quiet", action="count", default=0, help="Print less output. You can specify this option multiple times.")
|
||||
parser.add_option("-v", "--verbose", dest="verbose", action="count", default=0, help="Print more output. You can specify this option multiple times.")
|
||||
|
||||
@@ -98,6 +99,11 @@ def main():
|
||||
else:
|
||||
imgformat = 'png'
|
||||
|
||||
if options.optimizeimg:
|
||||
optimizeimg = options.optimizeimg
|
||||
else:
|
||||
optimizeimg = None
|
||||
|
||||
logging.getLogger().setLevel(
|
||||
logging.getLogger().level + 10*options.quiet)
|
||||
logging.getLogger().setLevel(
|
||||
@@ -111,7 +117,7 @@ def main():
|
||||
w.go(options.procs)
|
||||
|
||||
# Now generate the tiles
|
||||
q = quadtree.QuadtreeGen(w, destdir, depth=options.zoom, imgformat=imgformat)
|
||||
q = quadtree.QuadtreeGen(w, destdir, depth=options.zoom, imgformat=imgformat, optimizeimg=optimizeimg)
|
||||
q.go(options.procs)
|
||||
|
||||
def delete_all(worlddir, tiledir):
|
||||
|
||||
21
optimizeimages.py
Normal file
21
optimizeimages.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import os
|
||||
import subprocess
|
||||
import shlex
|
||||
|
||||
def optimize_image(imgpath, imgformat, optimizeimg):
|
||||
if imgformat == 'png':
|
||||
if optimizeimg == "1" or optimizeimg == "2":
|
||||
# we can't do an atomic replace here because windows is terrible
|
||||
# so instead, we make temp files, delete the old ones, and rename
|
||||
# the temp files. go windows!
|
||||
subprocess.Popen(shlex.split("pngcrush " + imgpath + " " + imgpath + ".tmp"),
|
||||
stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
|
||||
os.remove(imgpath)
|
||||
os.rename(imgpath+".tmp", imgpath)
|
||||
|
||||
if optimizeimg == "2":
|
||||
subprocess.Popen(shlex.split("optipng " + imgpath), stderr=subprocess.STDOUT,
|
||||
stdout=subprocess.PIPE).communicate()[0]
|
||||
subprocess.Popen(shlex.split("advdef -z4 " + imgpath), stderr=subprocess.STDOUT,
|
||||
stdout=subprocess.PIPE).communicate()[0]
|
||||
|
||||
24
quadtree.py
24
quadtree.py
@@ -24,10 +24,11 @@ import shutil
|
||||
import collections
|
||||
import json
|
||||
import logging
|
||||
import util
|
||||
|
||||
from PIL import Image
|
||||
from optimizeimages import optimize_image
|
||||
|
||||
import util
|
||||
|
||||
"""
|
||||
This module has routines related to generating a quadtree of tiles
|
||||
@@ -55,7 +56,7 @@ def catch_keyboardinterrupt(func):
|
||||
return newfunc
|
||||
|
||||
class QuadtreeGen(object):
|
||||
def __init__(self, worldobj, destdir, depth=None, imgformat=None):
|
||||
def __init__(self, worldobj, destdir, depth=None, imgformat=None, optimizeimg=None):
|
||||
"""Generates a quadtree from the world given into the
|
||||
given dest directory
|
||||
|
||||
@@ -67,6 +68,7 @@ class QuadtreeGen(object):
|
||||
"""
|
||||
assert(imgformat)
|
||||
self.imgformat = imgformat
|
||||
self.optimizeimg = optimizeimg
|
||||
|
||||
if depth is None:
|
||||
# Determine quadtree depth (midpoint is always 0,0)
|
||||
@@ -235,7 +237,8 @@ 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, self.imgformat))
|
||||
colstart, colend, rowstart, rowend, dest, self.imgformat,
|
||||
self.optimizeimg))
|
||||
|
||||
def _apply_render_inntertile(self, pool, zoom):
|
||||
"""Same as _apply_render_worltiles but for the inntertile routine.
|
||||
@@ -247,7 +250,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, self.imgformat))
|
||||
yield pool.apply_async(func=render_innertile, args= (dest, name, self.imgformat, self.optimizeimg))
|
||||
|
||||
def go(self, procs):
|
||||
"""Renders all tiles"""
|
||||
@@ -331,7 +334,7 @@ class QuadtreeGen(object):
|
||||
pool.join()
|
||||
|
||||
# Do the final one right here:
|
||||
render_innertile(os.path.join(self.destdir, "tiles"), "base", self.imgformat)
|
||||
render_innertile(os.path.join(self.destdir, "tiles"), "base", self.imgformat, self.optimizeimg)
|
||||
|
||||
def _get_range_by_path(self, path):
|
||||
"""Returns the x, y chunk coordinates of this tile"""
|
||||
@@ -362,7 +365,7 @@ class QuadtreeGen(object):
|
||||
return chunklist
|
||||
|
||||
@catch_keyboardinterrupt
|
||||
def render_innertile(dest, name, imgformat):
|
||||
def render_innertile(dest, name, imgformat, optimizeimg):
|
||||
"""
|
||||
Renders a tile at os.path.join(dest, name)+".ext" by taking tiles from
|
||||
os.path.join(dest, name, "{0,1,2,3}.png")
|
||||
@@ -452,12 +455,15 @@ def render_innertile(dest, name, imgformat):
|
||||
img.save(imgpath, quality=95, subsampling=0)
|
||||
else: # png
|
||||
img.save(imgpath)
|
||||
if optimizeimg:
|
||||
optimize_image(imgpath, imgformat, optimizeimg)
|
||||
|
||||
with open(hashpath, "wb") as hashout:
|
||||
hashout.write(newhash)
|
||||
|
||||
|
||||
@catch_keyboardinterrupt
|
||||
def render_worldtile(chunks, colstart, colend, rowstart, rowend, path, imgformat):
|
||||
def render_worldtile(chunks, colstart, colend, rowstart, rowend, path, imgformat, optimizeimg):
|
||||
"""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
|
||||
@@ -586,6 +592,10 @@ def render_worldtile(chunks, colstart, colend, rowstart, rowend, path, imgformat
|
||||
|
||||
# Save them
|
||||
tileimg.save(imgpath)
|
||||
|
||||
if optimizeimg:
|
||||
optimize_image(imgpath, imgformat, optimizeimg)
|
||||
|
||||
with open(hashpath, "wb") as hashout:
|
||||
hashout.write(digest)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user