-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract
executable file
·65 lines (51 loc) · 1.71 KB
/
extract
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
import os
import sys
import json
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import extcolors
from PIL import Image
def rgb_to_hex(r, g, b):
return '#{:02x}{:02x}{:02x}'.format(r, g, b)
def get_image_files(file):
if file.endswith('jpg') or file.endswith('png'):
return True
else:
return False
def resize_image(file_path):
image = Image.open(file_path)
file_name, extension = os.path.splitext(file_path)
size = image.size
new_image_height = 190
new_image_width = int(size[1] / size[0] * new_image_height)
image = image.resize((new_image_height, new_image_width), Image.ANTIALIAS)
os.remove(file_path)
image.save(file_path, 'PNG', quality=100)
def extract(image):
return extcolors.extract_from_path(image, tolerance = 13, limit = 70)
if __name__ == "__main__":
images = list(filter(get_image_files, os.listdir("./images")))
if len(images) == 0:
print("Geen afbeeldingen gevonden, plaats ze in de `images` map")
sys.exit(0)
for image in images:
print("resizing {}...".format(image))
resize_image('./images/' + image)
output = {}
for x, image in enumerate(images):
print("generating")
colors = extract("./images/" + image)[0]
occ_total = 0
for num in colors:
occ_total += num[1]
output[image] = []
for y, color in enumerate(colors):
hex = rgb_to_hex(color[0][0], color[0][1], color[0][2])
occ = color[1] / occ_total
output[image].append((hex, occ))
if os.path.exists("colors.json"):
os.remove("colors.json")
f = open("colors.json", "w")
f.write(json.dumps(output))