def run_benchmark(backend_names, sample_dir=None, output_dir=None): if not backend_names: backend_names = ('PIL', 'GraphicsMagick', 'Aware', 'java') backends = {} for backend_name in backend_names: try: backends[backend_name] = get_image_class(backend_name) except (ImportError, KeyError): print >> sys.stderr, "Can't load %s backend" % backend_name times = defaultdict(dict) if not os.path.exists(output_dir): os.makedirs(output_dir) print "Comparison images are saved in %s" % output_dir for filename in os.listdir(sample_dir): if filename.startswith("."): continue basename = os.path.splitext(filename)[0] for backend_name, backend_class in backends.items(): logging.info("Resizing %s using %s", filename, backend_name) start_time = time() try: master = backend_class.open(os.path.join(sample_dir, filename)) master.thumbnail((256, 256), backend_class.ANTIALIAS) output_file = os.path.join(output_dir, "%s_%s.jpg" % (basename, backend_name)) master.save(open(output_file, "wb"), "JPEG") times[filename][backend_name] = time() - start_time # This allows us to use a blanket except below which has the nice # property of catching direct Exception subclasses and things like # java.lang.Exception subclasses on Jython: except SystemExit: sys.exit(1) except: logging.exception("%s: exception processing %s", backend_name, filename) print print "Results" print for f_name, scores in times.items(): print "%s:" % f_name for lib, elapsed in scores.items(): print "\t%16s: %0.2f" % (lib, elapsed) print
#!/usr/bin/env python from collections import defaultdict from time import time import os import sys from NativeImaging import get_image_class BACKENDS = {} for backend_name in sys.argv[1:] or ('PIL', 'GraphicsMagick', 'Aware'): try: BACKENDS[backend_name] = get_image_class(backend_name) except KeyError: print >>sys.stderr, "Can't load %s backend" % backend_name SAMPLE_DIR = os.path.join(os.path.dirname(__file__), "samples") OUTPUT_DIR = os.path.join(os.environ.get("TMPDIR"), "resize-bench") TIMES = defaultdict(dict) if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) print "Comparison images are saved in %s" % OUTPUT_DIR for filename in os.listdir(SAMPLE_DIR): basename = os.path.splitext(filename)[0] for backend_name, backend_class in BACKENDS.items(): start_time = time()
from __future__ import absolute_import, division, print_function import unittest from NativeImaging import get_image_class from .api import ApiConformanceTests try: AWARE_IMAGE_CLASS = get_image_class("aware") except ImportError: AWARE_IMAGE_CLASS = None @unittest.skipUnless(AWARE_IMAGE_CLASS, 'Aware ctypes backend is not not available') class AwareTests(ApiConformanceTests, unittest.TestCase): IMAGE_CLASS = AWARE_IMAGE_CLASS if __name__ == "__main__": unittest.main()