-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgsource.py
More file actions
39 lines (31 loc) · 1.21 KB
/
imgsource.py
File metadata and controls
39 lines (31 loc) · 1.21 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
from syskaled.syskaled import RGB
import numpy
class ImageSourceBase():
""" Abstract Base class for theme color image source"""
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
@staticmethod
def get_dom_color_from_image(img):
""" Extracts most dominant color from image
:param img: PIL Image object
:returns RGB object representing r,g and b values
"""
img = img.resize((150, 150), resample=0)
pixels = img.getcolors(150 * 150)
sorted_pixels = sorted(pixels, key=lambda t: t[0])
dominant_color = sorted_pixels[-1][1][:3]
return RGB(*dominant_color)
@staticmethod
def get_avg_color_from_image(img):
""" Extracts average color from image
:param img: PIL Image object
:returns RGB object representing r,g and b values
"""
avg_color_per_row = numpy.average(img, axis=0)
avg_color = numpy.average(avg_color_per_row, axis=0)
rgb = list(map(lambda x: int(x), avg_color))
return RGB(*rgb)
def get_theme_color(self):
raise NotImplementedError("get_theme_color() should return a RGB(r,g,b) object")