-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorReducer.py
64 lines (57 loc) · 2.44 KB
/
ColorReducer.py
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
import sys
from Colors import TEN_COLORS as colors
from PIL import Image
from math import sqrt
from matplotlib.colors import to_rgba_array
# Used for debugging.
hex_value = True
try:
im = Image.open(sys.argv[1])
except:
raise("Error: cannot open image")
px = im.load()
if hex_value:
# This section of code iterates over the every pixel in the image,
# and converts its color to an rgb tuple. It will then calculate the
# euclidean distance (https://en.wikipedia.org/wiki/Euclidean_distance)
# of all of the colors and set the color of the pixel to be the color with the
# shortest distance. I'm not knowledgeable on color theory, but this is the best
# way to reduce colors that I could think of. As I learn more, I will create a less
# intensive, more efficient algorithm.
for x in range(im.size[1]):
for y in range(im.size[0]):
minColorDistance = -1
minColor = None
pixel = px[x,y]
for color in colors:
rgb = to_rgba_array(color)[0]
for i,val in enumerate(rgb):
rgb[i] = int(val * 255)
distance = sqrt((pixel[0] - rgb[0]) ** 2 + (pixel[1] - rgb[1]) ** 2 + (pixel[2] - rgb[2]) ** 2)
if minColorDistance == -1:
minColorDistance = distance
minColor = rgb
else:
if minColorDistance > distance:
minColorDistance = distance
minColor = rgb
px[x,y] = (int(minColor[0]), int(minColor[1]), int(minColor[2]))
else:
# Same concept as in the if clause, except the color coming in is already in rgb form.
# I should probably turn this into a function and then just call it from above.
for x in range(im.size[1]):
for y in range(im.size[0]):
minColorDistance = -1
minColor = None
pixel = px[x,y]
for color in colors:
distance = sqrt((pixel[0] - color[0]) ** 2 + (pixel[1] - color[1]) ** 2 + (pixel[2] - color[2]) ** 2)
if minColorDistance == -1:
minColorDistance = distance
minColor = color
else:
if minColorDistance > distance:
minColorDistance = distance
minColor = color
px[x,y] = (color[0], color[1], color[2])
im.show()