-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_image.py
156 lines (138 loc) · 5.14 KB
/
color_image.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import division
from PIL import Image
from grapefruit import Color
from util import interpolate, chroma_hue_to_ab
gray = Color.NewFromLab(50, 0, 0)
def color_to_ints(color):
# Gray out illegal colors.
if not color.isLegal:
color = gray
return color.intTuple
def draw_lab_l_slice(image, l):
width, height = image.size
pixels = image.load()
for x in range(width):
a = interpolate(-1.0, 1.0, x / width)
for y in range(height):
b = interpolate(-1.0, 1.0, y / height)
color = Color.NewFromLab(l, a, b)
pixels[x,y] = color_to_ints(color)
def draw_lab_a_slice(image, a):
width, height = image.size
pixels = image.load()
for x in range(width):
b = interpolate(-1.0, 1.0, x / width)
for y in range(height):
l = interpolate(99.9, 0.0, y / height)
color = Color.NewFromLab(l, a, b)
pixels[x,y] = color_to_ints(color)
def draw_lab_b_slice(image, b):
width, height = image.size
pixels = image.load()
for x in range(width):
a = interpolate(-1.0, 1.0, x / width)
for y in range(height):
l = interpolate(99.9, 0.0, y / height)
color = Color.NewFromLab(l, a, b)
pixels[x,y] = color_to_ints(color)
def draw_lab_l_cylinder(image, l):
width, height = image.size
pixels = image.load()
for x in range(width):
hue = interpolate(0, 360, x / width)
for y in range(height):
chroma = interpolate(1.4, 0.0, y / height)
a, b = chroma_hue_to_ab(chroma, hue)
color = Color.NewFromLab(l, a, b)
pixels[x,y] = color_to_ints(color)
def make_lab_slices():
for size in (16, 128, 512, 1024):
img = Image.new('RGB', (size, size), 'white')
for l in range(5, 100, 5):
draw_lab_l_slice(img, l)
name = 'CIELAB_{}_L{:02d}.png'.format(size, int(l))
print(name)
img.save(name)
for a_index in range(1, 19 + 1):
a = interpolate(-1.0, 1.0, a_index / 20)
draw_lab_a_slice(img, a)
name = 'CIELAB_{}_a{:02d}.png'.format(size, a_index)
print(name)
img.save(name)
for b_index in range(1, 19 + 1):
b = interpolate(-1.0, 1.0, b_index / 20)
draw_lab_b_slice(img, b)
name = 'CIELAB_{}_b{:02d}.png'.format(size, b_index)
print(name)
img.save(name)
def make_lab_cylinders_special():
special_colors = [
('red', (1, 0, 0)),
('green', (0, 1, 0)),
('blue', (0, 0, 1)),
('yellow', (1, 1, 0)),
('magenta', (1, 0, 1)),
('cyan', (0, 1, 1)),
]
for width, height in [(360, 100), (360*4, 100*4)]:
for color_name, rgb in special_colors:
l, a, b = Color.NewFromRgb(*rgb).lab
img = Image.new('RGB', (width, height), 'white')
draw_lab_l_cylinder(img, l)
name = 'CIELAB_CYLINDER_{}x{}_L{:02d}_{}.png'.format(width, height, int(l), color_name)
print(name)
img.save(name)
def make_lab_cylinders():
for width, height in [(360, 100), (360*4, 100*4)]:
for l in range(5, 100, 5):
img = Image.new('RGB', (width, height), 'white')
draw_lab_l_cylinder(img, l)
name = 'CIELAB_CYLINDER_{}x{}_L{:02d}.png'.format(width, height, int(l))
print(name)
img.save(name)
def draw_lab_hue_spoke(image, hue):
width, height = image.size
pixels = image.load()
for x in range(width):
chroma = interpolate(-1.5, 1.5, x / width)
for y in range(height):
l = interpolate(99.9, 0.0, y / height)
a, b = chroma_hue_to_ab(chroma, hue)
color = Color.NewFromLab(l, a, b)
pixels[x,y] = color_to_ints(color)
def make_lab_spokes():
for width, height in [(64, 64), (512, 512)]:
for hue in range(0, 180, 5):
img = Image.new('RGB', (width, height), 'white')
draw_lab_hue_spoke(img, hue)
name = 'CIELAB_CYLINDER_{}x{}_hue{:03d}.png'.format(width, height, hue)
print(name)
img.save(name)
def draw_cylinder_surface(image):
from limits import max_chroma
width, height = image.size
pixels = image.load()
for x in range(width):
hue = interpolate(0, 360, x / width)
for y in range(height):
l = interpolate(99.9, 0.0, y / height)
chroma = max_chroma(hue, l)
if chroma:
a, b = chroma_hue_to_ab(chroma, hue)
color = Color.NewFromLab(l, a, b)
else:
color = gray
pixels[x,y] = color_to_ints(color)
def make_cylinder_surfaces():
for width, height in [(36, 10), (360, 100), (360*4, 100*4)]:
img = Image.new('RGB', (width, height), 'white')
draw_cylinder_surface(img)
name = 'CIELAB_CYLINDER_SURFACE_{}x{}.png'.format(width, height)
print(name)
img.save(name)
if __name__ == '__main__':
#make_lab_slices()
#make_lab_cylinders()
make_lab_spokes()
#make_lab_cylinders_special()
#make_cylinder_surfaces()