Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (63 loc) · 2.27 KB
/
Copy pathapp.py
File metadata and controls
94 lines (63 loc) · 2.27 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import numpy as np
from PIL import Image
from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/Users/frank/Pictures/'
app.config['MAX_CONTENT_PATH'] = 10000000
# https://levelup.gitconnected.com/reading-and-analyzing-image-file-data-and-colors-with-python-from-matrices-to-web-applications-d18010f8966f
@app.route('/')
def home():
return render_template("index.html")
@app.route("/submit", methods=["GET", "POST"])
def submit():
if request.method == "POST":
f = request.files['file']
filename = secure_filename(f.filename)
if len(filename) > 1:
fullpath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
f.save(fullpath)
palette_hex, palette_rgb = get_colors_in_image(fullpath)
return render_template("index.html", hex_success=True, palette_hex=palette_hex, palette_rgb=palette_rgb)
return render_template("index.html")
def get_colors_in_image(filepath):
def rgb_to_hex(r, g, b):
ans = ('{:X}{:X}{:X}').format(r, g, b)
while len(ans) < 6:
ans = "0" + ans
return "#" + ans
def hex_to_rgb(h):
rgb = []
for i in (0, 2, 4):
decimal = int(h[i:i + 2], 16)
rgb.append(decimal)
return tuple(rgb)
def get_top_10(hex_list):
hex_frequency = {}
for item in hex_list:
if item in hex_frequency:
hex_frequency[item] += 1
else:
hex_frequency[item] = 1
sorted_hex = dict(
sorted(hex_frequency.items(), key=lambda item: item[1]))
return list(sorted_hex.keys())[-10:][::-1]
image_file = Image.open(filepath)
image_array = np.array(image_file)
shape = image_array.shape
x = shape[0]
y = shape[1]
hex_list = []
for x in range(x):
for y in range(y):
rgb = image_array[x, y, :]
r = rgb[0]
g = rgb[1]
b = rgb[2]
hex_list.append(rgb_to_hex(r, g, b))
top_10_hex = get_top_10(hex_list)
top_10_rgb = [hex_to_rgb(i[-6:]) for i in top_10_hex]
return top_10_hex, top_10_rgb
if __name__ == "__main__":
app.run(debug=True)