Skip to content

Instantly share code, notes, and snippets.

@feihong
Created October 6, 2015 21:38
Show Gist options
  • Save feihong/a52604c7eb4521ce81cd to your computer and use it in GitHub Desktop.
Save feihong/a52604c7eb4521ce81cd to your computer and use it in GitHub Desktop.
Generate a palette image file
from PIL import Image, ImageDraw
length = 100
columns = 4
colors = """
whitesmoke gainsboro gray black
indianred lightpink tomato lemonchiffon
plum palegreen paleturquoise burlywood
peachpuff khaki slateblue teal
""".split()
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in xrange(0, len(l), n):
yield l[i:i+n]
colors = list(chunks(colors, 4))
width = columns * length
height = len(colors) * length
img = Image.new('RGBA', (width, height), (255,255,255,0))
draw = ImageDraw.Draw(img)
for i, row in enumerate(colors):
for j, color in enumerate(row):
x0 = j * 100
y0 = i * 100
x1 = x0 + 100
y1 = y0 + 100
draw.rectangle([x0, y0, x1, y1], fill=color)
img.save('palette.png', 'PNG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment