-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathAn_Identiconizer_generator_implementation_in_Python.py
304 lines (197 loc) · 7 KB
/
An_Identiconizer_generator_implementation_in_Python.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# coding: utf-8
# # An Identiconizer generator implementation in Python
#
# This small notebook implements a generator of small square icons like the ones in GitHub, as [implemented by identicon.js](https://github.com/stewartlord/identicon.js#identiconjs).
# ## The function
# In[5]:
import random
import numpy as np
from matplotlib.colors import hsv_to_rgb
from PIL import Image as PILImage, ImageDraw as PILImageDraw
# In[6]:
get_ipython().run_line_magic('load_ext', 'watermark')
get_ipython().run_line_magic('watermark', '')
get_ipython().run_line_magic('watermark', '-a "Lilian Besson (Naereen)" -p numpy,matplotlib,PIL')
# ## First try
# In[8]:
def identicon(hashval=None,
size=256,
margin=0.11,
foreground=(255,0,0,255),
background=(240,240,240,255),
saturation=0.7,
brightness=0.5,
xsym=True,
):
if hashval is not None:
if isinstance(hashval, str):
hashval = abs(hash(hashval))
random.seed(int(hashval))
hue = random.random()
foreground = tuple(np.array(hsv_to_rgb([hue, saturation, brightness]) * 256, dtype=int))
border = int(size * margin)
ampl = size - 2 * border
def cropfirst(x):
return max(border, min(border + x, size - border))
def cropsecond(x):
return max(border, min(size - border - x, size - border))
# make a blank image
im = PILImage.new('RGBA', (size,size), background)
# get a drawing context
d = PILImageDraw.Draw(im)
def rect(xy):
x1, y1, x2, y2 = xy
r1 = (cropfirst(x1), cropfirst(y1), cropfirst(x2), cropfirst(y2))
if xsym:
r2 = (cropsecond(x1), cropfirst(y1), cropsecond(x2), cropfirst(y2))
else:
r2 = (cropfirst(x1), cropsecond(y1), cropfirst(x2), cropsecond(y2))
d.rectangle(r1, fill=foreground)
d.rectangle(r2, fill=foreground)
nbrectangle = random.randint(4, 8)
for i in range(nbrectangle):
x1, y1 = [random.randint(0, ampl) for _ in range(2)]
dx, dy = [random.randint(ampl // 8, ampl // 2) for _ in range(2)]
dirx, diry = [random.choice([+1, -1]) for _ in range(2)]
rect((x1, y1, x1 + dirx * dx, y1 + diry * dy))
return im
# In[9]:
im = identicon()
# In[10]:
im
# ## Second try
# In[11]:
from itertools import product
# In[12]:
def identicon2(hashval=None,
size=256,
nbsquares=5,
margin=0.09,
foreground=(255,0,0,255),
background=(240,240,240,255),
saturation=0.7,
brightness=0.5,
xsym=True,
):
if hashval is not None:
if isinstance(hashval, str):
hashval = abs(hash(hashval))
random.seed(int(hashval) % 1<<28)
np.random.seed(int(hashval) % 1<<28)
hue = random.random()
foreground = tuple(np.array(hsv_to_rgb([hue, saturation, brightness]) * 256, dtype=int))
border = int(size * margin)
ampl = size - 2 * border
size_square = ampl // nbsquares
def cropfirst(x):
return max(border, min(border + x, size - border))
def cropsecond(x):
return max(border, min(size - border - x, size - border))
# make a blank image
im = PILImage.new('RGBA', (size,size), background)
# get a drawing context
d = PILImageDraw.Draw(im)
def rect(xy, xsym=xsym):
x1, y1, x2, y2 = xy
r1 = (cropfirst(x1), cropfirst(y1), cropfirst(x2), cropfirst(y2))
if xsym:
r2 = (cropsecond(x1), cropfirst(y1), cropsecond(x2), cropfirst(y2))
else:
r2 = (cropfirst(x1), cropsecond(y1), cropfirst(x2), cropsecond(y2))
d.rectangle(r1, fill=foreground)
d.rectangle(r2, fill=foreground)
nbrectangle = random.randint(5, nbsquares * (nbsquares // 2 + 1) - 2)
possible_coordinates = [(x, y) for (x, y) in product(range(nbsquares), range(nbsquares // 2 + 1))]
for i in np.random.choice(len(possible_coordinates), size=nbrectangle, replace=False):
y, x = possible_coordinates[i]
rect((x * size_square, y * size_square, (x+1) * size_square, (y+1) * size_square))
return im
# In[13]:
im = identicon2()
# In[14]:
im
# ## Tests
# This second implementation is better: it is deterministic, when given a string or a hash:
# In[15]:
for _ in range(2):
im = identicon2("Naereen")
im
# And every parameter can be changed and tuned.
# In[16]:
for _ in range(10):
im = identicon2(size=128)
im
# In[39]:
mylist = [identicon2() for _ in range(10)]
# ## List of vignettes
#
# Let's use [this technique](https://carreau.github.io/posts/29-JupyterCon-DisplayProtocol.ipynb.html)
# In[18]:
import base64
from IPython.display import Image
from IPython.display import HTML
def tag_from_data(data, size='100%'):
return (
'''<img
style="display:inline;
width:{1};
max-width:400px;
padding:10px;
margin-top:14px"
src="data:image/png;base64,{0}"
/>
''').format(''.join(base64.encodebytes(data).decode().split('\n')), size)
# In[19]:
import io
def im2bytes(im):
buffer = io.BytesIO()
im.save(buffer, format="png")
return buffer.getvalue()
# In[20]:
HTML(tag_from_data(im2bytes(im)))
# In[21]:
class VignetteList:
def __init__(self, *images, size=None):
self.images = images
self.size = size
def _repr_html_(self):
return '<p>'+','.join(tag_from_data(im2bytes(im), self.size) for im in self.images)+'</p>'
# In[41]:
VignetteList(*mylist, size='200px')
# In[42]:
def tag_from_data_II(data, size='100%'):
return '''<img
style="
display:inline;
width:{1};
max-width:400px;
padding:10px;
margin-top:14px"
onMouseOver="this.style['box-shadow']='5px 5px 30px 0px rgba(163,163,163,1)'"
onMouseOut="this.style['box-shadow']=''"
src="data:image/png;base64,{0}"
/>'''.format(''.join(base64.encodebytes(data).decode().split('\n')), size)
def html_list_formatter(ll):
html = get_ipython().display_formatter.formatters['text/html']
reps = []
for o in ll:
if isinstance(o, Image):
reps.append(tag_from_data_II(o.data, '200px') )
elif isinstance(im, PILImage.Image):
reps.append(tag_from_data_II(im2bytes(o), '200px') )
else:
h = html(o)
if h:
reps.append(h)
else:
reps.append(repr(o)+'')
return '<span>['+','.join(reps)+']</span>'
# In[43]:
ipython = get_ipython()
html = ipython.display_formatter.formatters['text/html']
html.for_type(list, html_list_formatter)
# In[44]:
mylist
# ## Conclusion
#
# > *That's it for today, folks!* [See on my GitHub for more notebooks](https://github.com/Naereen/notebooks)