-
Notifications
You must be signed in to change notification settings - Fork 6
/
imagesimpler.py
79 lines (68 loc) · 3.06 KB
/
imagesimpler.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
from os import path
import Image
import webcolors
class ImageSimpler():
"""
Purpose: To take an image and "round" all colors to one of a specified list of colors and then return the mutated image
Inputs: image to be manipulated, colors to be "rounded to", scaled size of the image
Outputs: The mutated image
"""
def simplify(self, imagefile, colors, percentsize):
"""
Purpose: To take an image, scale it as specified, and "round" all colors to one of a specified list of colors and then return the mutated image
Inputs: Image to be manipulated, colors to be "rounded to", scaled size of the image
Outputs: The mutated image
"""
n, e = path.splitext(imagefile)
if e.lower() not in ['.jpg', '.jpeg', '.png']:
raise Exception('Image type not supported')
if e.lower() == '.png':
Image.open(imagefile).save(n + '.jpg')
self.image = Image.open(n + '.jpg')
if percentsize is not 100:
self.image = self.scale_by_percent(self.image, percentsize)
self.color_map = self.map_RGB_to_color_word(colors)
#makes the image more readily mutable and readable by creating a "Pixel Access Object"
self.pixels = self.image.load()
return self.reduce_to_requested_color_space(self.pixels, self.image, self.color_map)
def scale_by_percent(self, im, percent):
"""
Purpose: Scale an image to a specified size
Inputs: Image, desired percent size
Outputs: Scaled image
"""
newx = int(im.size[0] * (percent/100.00))
newy = int(im.size[1] * (percent/100.00))
return im.resize((newx, newy))
def map_RGB_to_color_word(self, colors):
"""
Purpose: Creates a mapping of rgb colors to "css colors"
Inputs: List of css colors
Outputs: Dictionary of rgbs to css colors
"""
rgbsToColors = {}
for color in colors:
rgbsToColors[webcolors.name_to_rgb(color)] = color
return rgbsToColors
def reduce_to_requested_color_space(self, pix_obj, im, color_mapping):
"""
Purpose: Creates an image in which colors are changed to be whatever they are closest to from a list of colors
Inputs: PIL pixel object, image, dictionary of rgbs to css colors
Outputs: Color-simplified image
"""
print pix_obj
for x in range(im.size[0]):
for y in range(im.size[1]):
pix = pix_obj[x,y]
#initial settings soon to be overridden
closest = None
mindist = float('Inf')
#find which rgb triplet is the closest through Euclidean distance of their vectors
for rgb in color_mapping:
dist = (rgb[0] - pix[0])*(rgb[0]-pix[0]) + (rgb[1]-pix[1])*(rgb[1]-pix[1]) + (rgb[2]-pix[2])*(rgb[2]-pix[2])
if dist < mindist:
mindist = dist
closest = rgb
pix_obj[x,y] = closest
return im