-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
141 lines (114 loc) · 4.35 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import math
from pathlib import Path
from PIL import Image
from PIL import ImageCms
from PIL import ImageStat
from PIL.Image import Dither
from PIL.Image import Quantize
from Pylette import extract_colors
from Pylette import Palette
from tqdm import tqdm
from datetime import datetime
pic1 = Path('res/pic1.png')
pic2 = Path('res/pic2.jpg')
pic3 = Path('res/pic3.jpg')
pic4 = Path('res/pic4.jpg')
srgb_profile = ImageCms.createProfile("sRGB")
lab_profile = ImageCms.createProfile("LAB")
Color = (int, int, int) # 3 channels
def calc_rgb_similarity(color_1: Color, color_2: Color) -> float:
return math.sqrt(sum((
(color_1[0] - color_2[0]) ** 2,
(color_1[1] - color_2[1]) ** 2,
(color_1[2] - color_2[2]) ** 2,
)))
def calc_lab_similarity(color_1: Color, color_2: Color) -> float:
return math.sqrt(sum((
# skip L (luminance) channel
(color_1[1] - color_2[1]) ** 2,
(color_1[2] - color_2[2]) ** 2,
)))
def look_for_replacement_color(palette: Palette, color: (int, int, int)) -> (int, int, int):
# finds most similar color from given palette
result = palette.colors[0]
similarity = None
for c in palette.colors:
# print('color', c)
# r = c.rgb[0]
# g = c.rgb[1]
# b = c.rgb[2]
# calculated_similarity = calc_rgb_similarity(c.rgb, color)
calculated_similarity = calc_lab_similarity(c.rgb, color)
if similarity is None or calculated_similarity < similarity:
result = (color[0], c.rgb[1], c.rgb[2])
similarity = calculated_similarity
# print(f'{color} replaced with {result.rgb}')
return result
def convert_to_lab(img: Image) -> Image:
rgb2lab_transform = ImageCms.buildTransformFromOpenProfiles(
srgb_profile,
lab_profile,
"RGB",
"LAB",
)
return ImageCms.applyTransform(img, rgb2lab_transform)
# return ImageCms.profileToProfile(img, srgb_profile, lab_profile)
def extract_palette(img_path: Path) -> Palette:
palette = extract_colors(img_path, palette_size=10, resize=False)
print(palette)
# palette.display(save_to_file=False)
return palette
def main():
im1 = Image.open(pic4)
im1 = im1.convert('RGB')
print(im1.mode)
start_time = datetime.now()
# im1 = im1.resize((512, 512)) # enhance performance
# im1.quantize(colors=32).show()
# im1.quantize(colors=32, dither=Dither.NONE).show()
# im1.quantize(colors=16, method=Quantize.FASTOCTREE, dither=Dither.FLOYDSTEINBERG).show()
# im1.quantize(colors=20, method=Quantize.FASTOCTREE, dither=Dither.NONE).show()
# print(im1.mode, im1.getpixel((0, 0)))
# im1 = convert_to_lab(im1)
# print(im1.mode, im1.getpixel((0, 0)))
# im1.show()
# im2 = im1.quantize(colors=32)
# im1.quantize(colors=8).show()
im_size = im1.size
# merged_image = Image.new('RGB', (2 * im_size[0], im_size[1]), (0, 0, 0))
# merged_image.paste(im1, (0, 0))
# merged_image.paste(im2, (im_size[0], 0))
# merged_image.show()
palette = extract_palette(pic1)
# im1.point(lambda x: print("wtf", x))
img_pixels = im1.load()
for i in tqdm(range(im_size[0])):
for j in range(im_size[1]):
color = img_pixels[i, j]
# print('color', color)
img_pixels[i, j] = look_for_replacement_color(palette, color)
# print()
print('Done!')
print('Time:', (datetime.now() - start_time).total_seconds(), 'sec')
im1.show()
im1.save('last_gen', format='png')
stats = ImageStat.Stat(im1)
print(f'mean {stats.mean}, median {stats.median}, var {stats.var}, stddev {stats.stddev}')
# print(f'hist {im2.histogram()}')
#
# # snippet from github
# rgb_lab_transform = PIL.ImageCms.buildTransformFromOpenProfiles(PIL.ImageCms.createProfile('sRGB'), PIL.ImageCms.createProfile('LAB'), 'RGB', 'LAB')
# img = PIL.ImageCms.applyTransform(Image.open('filename.png').convert(mode='RGB'), rgb_lab_transform)
#
# # snippet from stackoverflow
# im2 = pyCMS.profileToProfile(im, pyCMS.createProfile("sRGB"), pyCMS.createProfile("LAB"))
#
# # another stackoverflow snippet
# from PIL import Image, ImageCms
# srgb_p = ImageCms.createProfile("sRGB")
# lab_p = ImageCms.createProfile("LAB")
#
# rgb2lab = ImageCms.buildTransformFromOpenProfiles(srgb_p, lab_p, "RGB", "LAB")
# Lab = ImageCms.applyTransform(im, rgb2lab)
if __name__ == '__main__':
main()