Skip to content

Instantly share code, notes, and snippets.

@cmllr
Last active September 19, 2020 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmllr/c2be20b85e85a59b36a114cd04f06673 to your computer and use it in GitHub Desktop.
Save cmllr/c2be20b85e85a59b36a114cd04f06673 to your computer and use it in GitHub Desktop.
Color palette identification, Modul Interkulturelle Kommunikation @ HS-KL
import colorgram
from PIL import Image, ImageDraw
from math import floor
inputs = [
'img/meal/ferrero_de.png',
'img/meal/nestle_cn.png',
'img/meal/nestle_sa.png',
'img/auto/bmw_de.png',
'img/auto/audi_cn.png',
'img/auto/renault_sa.png',
]
IMAGE_HEIGHT = 30
IMAGE_WIDTH = 300
for input_file in inputs:
colors = colorgram.extract(input_file, 30)
img = Image.new('RGB', (IMAGE_WIDTH, IMAGE_HEIGHT), "white")
x = 0
pixels = img.load()
draw = ImageDraw.Draw(img)
for color in colors:
width = floor(color.proportion * IMAGE_WIDTH)
draw.rectangle(((x, 0), (x + width, IMAGE_HEIGHT)), fill=color.rgb)
x = x + width
file_name = input_file.replace(".png", "_palette.png")
img.save(file_name, "PNG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment