This is a markdown file to explain how the script works
Last active
October 1, 2015 04:28
-
-
Save mattalcock/1920167 to your computer and use it in GitHub Desktop.
python script to process images from the web very quickly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 as urllib | |
import json | |
from time import time | |
from cStringIO import StringIO | |
from collections import defaultdict | |
import Image,ImageStat #pip install pil | |
perf = defaultdict (list) | |
#Large image | |
imageurl = "http://bit.ly/wOqVTE" | |
def log_performance(func): | |
def wrapper(*args, **kwargs): | |
start = time() | |
r = func(*args, **kwargs) | |
elapsed = time() - start | |
message = { | |
'ts': int(time()), | |
'elapsed': elapsed, | |
'function': func.__name__, | |
'args': [str(arg) for arg in args], #str so can be json dumpable. | |
'kwargs': {k: str(v) for k,v in kwargs.iteritems()}, | |
} | |
perf[ func.__name__].append(elapsed) | |
#print("json: " + json.dumps(message)) | |
return r # return results untouched. | |
return wrapper | |
@log_performance | |
def get_image(imageurl): | |
img_file = urllib.urlopen(imageurl) | |
data = StringIO(img_file.read()) | |
im = Image.open(data) | |
size = 128, 128 | |
im.thumbnail(size, Image.ANTIALIAS) | |
return im | |
@log_performance | |
def process_image(image, sample_limit=10000, top=10): | |
colors = image.getcolors(sample_limit) | |
sc = sorted(colors, key=lambda x: x[0], reverse=True) | |
return sc[:top] | |
if __name__ == "__main__": | |
tests = 20 | |
for x in xrange(tests): | |
i = get_image(imageurl) | |
r = process_image(i) | |
print r | |
for k, v in perf.iteritems(): | |
l = len(v) | |
print "Function: %s, Executed:%s, Avg Time:%s" % (k, l, sum(v)/l) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment