-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBtoHSL.py
62 lines (36 loc) · 1.3 KB
/
RGBtoHSL.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
from PIL import Image
def convert_image(image_path):
image = Image.open(image_path)
if image.mode != 'RGB':
image = image.convert('RGB')
width, height = image.size
hsl_image = Image.new(image.mode, image.size)
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
h, s, l = rgb_to_hsl(r, g, b)
hsl_image.putpixel((x, y), (int(h), int(s * 255), int(l * 255)))
hsl_image.save('hsl_image.jpg')
def rgb_to_hsl(r, g, b):
r /= 255
g /= 255
b /= 255
max_color = max(r, g, b)
min_color = min(r, g, b)
if max_color == min_color:
hue = 0
elif max_color == r:
hue = (60 * (g - b) / (max_color - min_color)) % 360
elif max_color == g:
hue = 60 * (b - r) / (max_color - min_color) + 120
elif max_color == b:
hue = 60 * (r - g) / (max_color - min_color) + 240
lightness = (max_color + min_color) / 2
if lightness == 0 or max_color == min_color:
saturation = 0
elif lightness > 0 and lightness <= 0.5:
saturation = (max_color - min_color) / (2 * lightness)
elif lightness > 0.5:
saturation = (max_color - min_color) / (2 - 2 * lightness)
return (hue, saturation, lightness)
convert_image('picture1.jpg')