1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#!/usr/bin/env python3
""" Calculate the average color of an image
Code adapted from from: https://github.com/ZeevG/python-dominant-image-colour
"""
import binascii
import struct
try:
import Image, ImageDraw
except ImportError:
from PIL import Image, ImageDraw
def get_dom_color(filename):
image = Image.open(filename)
image = image.resize((150, 150)) # optional, to reduce time
colour_tuple = [None, None, None]
for channel in range(3):
# Get data for one channel at a time
# in case of errors stop processing and return black as the
# dominant colour
try:
pixels = image.getdata(band=channel)
except ValueError:
return "000000"
values = []
for pixel in pixels:
values.append(pixel)
colour_tuple[channel] = int(sum(values) / len(values))
colour = binascii.hexlify(struct.pack('BBB', *colour_tuple)).decode('utf-8')
return colour
|