Skip to content

Instantly share code, notes, and snippets.

@jbaiter
Created January 24, 2014 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaiter/8596064 to your computer and use it in GitHub Desktop.
Save jbaiter/8596064 to your computer and use it in GitHub Desktop.
from __future__ import division
import timeit
from StringIO import StringIO # needed for PIL
import requests
from pylab import *
from jpegtran import JPEGImage as jpegtranImage
from wand.image import Image as wandImage
from PIL import Image as pilImage
IMGDATA = requests.get("http://upload.wikimedia.org/wikipedia/commons/8/82/"
"Mandel_zoom_05_tail_part.jpg").content
def wand_scale(imgdata):
wimg = wandImage(blob=imgdata)
wimg.sample(200, 150)
return wimg.make_blob()
def pil_scale(imgdata):
scaled = pilImage.open(StringIO(imgdata)).resize((200, 150))
out = StringIO()
scaled.save(out, "JPEG")
return out.getvalue()
def jpegtran_scale(imgdata):
return jpegtranImage(blob=imgdata).downscale(200, 150).as_blob()
def wand_rotate(imgdata):
wimg = wandImage(blob=imgdata)
wimg.rotate(90)
return wimg.make_blob()
def pil_rotate(imgdata):
# somehow PIL does rotation differently....
rotated = pilImage.open(StringIO(imgdata)).rotate(-90)
out = StringIO()
rotated.save(out, "JPEG")
return out.getvalue()
def jpegtran_rotate(imgdata):
jpegtranImage(blob=imgdata).rotate(90).as_blob()
def wand_crop(imgdata):
wimg = wandImage(blob=imgdata)
wimg.crop(left=0, top=0, width=500, height=500)
return wimg.make_blob()
def pil_crop(imgdata):
cropped = pilImage.open(StringIO(imgdata)).crop((0, 0, 500, 500))
out = StringIO()
cropped.save(out, "JPEG")
return out.getvalue()
def jpegtran_crop(imgdata):
jpegtranImage(blob=imgdata).crop(0, 0, 500, 500).as_blob()
def bench(cmd):
return min(timeit.repeat(
stmt="{0}(IMGDATA)".format(cmd),
setup="from __main__ import {0}, IMGDATA".format(cmd),
repeat=3, number=10))/10*1000
jpegtran = (
bench("jpegtran_scale"),
bench("jpegtran_rotate"),
bench("jpegtran_crop")
)
pil = (
bench("pil_scale"),
bench("pil_rotate"),
bench("pil_crop")
)
wand = (
bench("wand_scale"),
bench("wand_rotate"),
bench("wand_crop")
)
n_groups = 3
fig, ax = subplots()
index = arange(n_groups)
bar_width = 0.3
print "Wand: {0}".format(wand)
print "PIL: {0}".format(pil)
print "jpegtran: {0}".format(jpegtran)
opacity = 0.7
rects1 = barh(index, jpegtran, bar_width,
alpha=opacity,
color='r',
label='jpegtran')
rects2 = barh(index + bar_width, wand, bar_width,
alpha=opacity,
color='b',
label='wand-py')
rects3 = barh(index + 2*bar_width, pil, bar_width,
alpha=opacity,
color='g',
label='PIL/Pillow')
xlabel('Execution time (ms)')
yticks(index + bar_width,
('scale to 250x150', 'rotate 90 degrees CW', 'crop 500x500 from 0,0'))
legend()
tight_layout()
savefig('bench.png')
#show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment