Skip to content

Instantly share code, notes, and snippets.

@tomdyson
Created September 27, 2013 14:55
Show Gist options
  • Select an option

  • Save tomdyson/6729882 to your computer and use it in GitHub Desktop.

Select an option

Save tomdyson/6729882 to your computer and use it in GitHub Desktop.
favicons with extracted colours
from colorific import extract_colors
from django_rq import job
from PIL import Image as Im
from PIL import ImageChops, ImageDraw
@job
def populate_colours(photo_id):
from models import Colour, Photo
# remove any existing colours
photo = Photo.objects.get(id=photo_id)
for colour in Colour.objects.filter(photo=photo):
colour.delete()
palette = extract_colors(photo.image, max_colors=5)
hex_list = []
for colour in palette.colors:
hex = '%.02x%.02x%.02x' % colour.value
hex_list.append(hex)
prominence = colour.prominence
c = Colour(hex=hex, prominence=prominence, photo=photo)
c.save()
return ",".join(hex_list)
def generate_favicon(photo_id):
from models import Colour
size = (40, 40)
img = Im.new('RGB', size)
draw = ImageDraw.Draw(img)
colours = Colour.objects.filter(photo_id=photo_id).order_by('-prominence')
for i, colour in enumerate(colours):
(x1, y1) = (i * 8, 0)
(x2, y2) = ((i + 1) * 8 - 1, 39)
draw.rectangle([(x1, y1), (x2, y2)], fill="#%s" % (colour.hex))
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment