Last active
September 19, 2020 14:33
-
-
Save cmllr/c2be20b85e85a59b36a114cd04f06673 to your computer and use it in GitHub Desktop.
Color palette identification, Modul Interkulturelle Kommunikation @ HS-KL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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