Skip to content

Instantly share code, notes, and snippets.

@williame
Last active August 29, 2015 14:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williame/92c7179d0963553d605f to your computer and use it in GitHub Desktop.
Save williame/92c7179d0963553d605f to your computer and use it in GitHub Desktop.
import os, sys
from PIL import Image
size = 128, 128
w, h = size
reds = [0] * w * h
greens = [0] * w * h
blues = [0] * w * h
count = 0
path = sys.argv[1] if len(sys.argv) > 1 else "."
try:
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in filenames:
if os.path.splitext(filename)[1].lower() in ('.jpg', '.png'):
path = os.sep.join([dirpath, filename])
try:
im = Image.open(path).convert("RGB").resize(size,
Image.ANTIALIAS)
except IOError:
print "could not load", path
continue
count += 1
print "adding", path
for i, (r, g, b) in enumerate(im.getdata()):
reds[i] += r
greens[i] += g
blues[i] += b
except KeyboardInterrupt:
print
print "added", count, "images"
if count:
def norm(channel):
channel = [c / count for c in channel]
mn = min(channel)
scale = 255 * 1 / (max(channel) - mn)
for c in channel:
yield (c - mn) * scale
data = bytearray([0] * w * h * 3)
data[0::3] = norm(reds)
data[1::3] = norm(greens)
data[2::3] = norm(blues)
dest = Image.frombuffer("RGB", size, buffer(data), 'raw', "RGB", 0, 1)
dest.show()
dest.save("emergent_orange.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment