-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaic.py
executable file
·381 lines (327 loc) · 10.4 KB
/
mosaic.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python
import argparse
import ctypes
import sys
from contextlib import contextmanager
from math import exp, sqrt
import sdl2
from OpenGL.GL import (
GL_BLEND,
GL_CLAMP,
GL_COLOR_BUFFER_BIT,
GL_COMPILE,
GL_DECAL,
GL_FLAT,
GL_MODELVIEW,
GL_NEAREST,
GL_ONE_MINUS_SRC_ALPHA,
GL_PROJECTION,
GL_QUADS,
GL_REPEAT,
GL_RGBA,
GL_SRC_ALPHA,
GL_TEXTURE_2D,
GL_TEXTURE_ENV,
GL_TEXTURE_ENV_MODE,
GL_TEXTURE_MAG_FILTER,
GL_TEXTURE_MIN_FILTER,
GL_TEXTURE_WRAP_S,
GL_TEXTURE_WRAP_T,
GL_UNPACK_ALIGNMENT,
GL_UNSIGNED_BYTE,
glBegin,
glBindTexture,
glBlendFunc,
glCallList,
glClear,
glClearColor,
glColor4f,
glEnable,
glEnd,
glEndList,
glGenLists,
glGenTextures,
glLoadIdentity,
glMatrixMode,
glNewList,
glOrtho,
glPixelStorei,
glPopMatrix,
glPushMatrix,
glRotatef,
glScalef,
glShadeModel,
glTexCoord2f,
glTexEnvf,
glTexImage2D,
glTexParameterf,
glTranslatef,
glVertex2f,
glViewport,
)
from graph import image_iterator
from mosaicfactory import MosaicFactory
parser = argparse.ArgumentParser(description="Photos mosaic visualization")
parser.add_argument("folder", type=str, help="folder containing photos")
parser.add_argument(
"-t", "--tiles", type=int, default=40, help="number of tiles in each mosaic"
)
parser.add_argument(
"-p",
"--pixels-limit",
type=int,
default=640 * 480,
help="maximum number of pixels for each texture (defaults to 640x480)",
)
parser.add_argument(
"-d", "--duration", type=float, default=10.0, help="zooming out duration in seconds"
)
parser.add_argument(
"-n",
"--no-reuse",
dest="reuse",
action="store_false",
help="a tile can only be used once in a photo (this requires that tiles²"
" <= #photos in folder",
)
def find_picture_in_mosaic(picture, mosaic):
x = -1
for y, line in enumerate(mosaic):
if picture in line:
x = line.index(picture)
break
if x == -1:
raise Exception("picture not in mosaic")
return (x, len(mosaic) - y - 1)
@contextmanager
def limit_pixels_count(img, limit):
pixels_count = img.width * img.height
if pixels_count > limit:
ratio = sqrt(limit / float(pixels_count))
new_width = int(round(img.width * ratio))
new_height = int(round(img.height * ratio))
with img.resized(new_width, new_height) as image:
yield image
else:
with img.open_image() as image:
yield image
def load_texture(img):
with limit_pixels_count(img, args.pixels_limit) as image:
width, height = image.size
image = image.tobytes("raw", "RGBX", 0, -1)
# Create Texture
_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, _id)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(
GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image
)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
return _id
def generate_picture_display_list(picture, width, height):
dl = glGenLists(1)
picture_display_lists[picture] = dl
glNewList(dl, GL_COMPILE)
draw_picture(picture, 0, 0, width, height)
glEndList()
def generate_mosaic_display_list(picture):
dl = glGenLists(1)
mosaic_display_lists[picture] = dl
glNewList(dl, GL_COMPILE)
draw_mosaic(picture)
glEndList()
def draw_mosaic(picture):
m = mosaic_factory.cached_mosaic(picture, args.tiles, args.reuse)
for column in range(args.tiles):
for line in range(args.tiles):
glPushMatrix()
glTranslatef(column * mosaic_factory.ratio * size, line * size, 0.0)
glCallList(picture_display_lists[m[args.tiles - 1 - line][column]])
glPopMatrix()
def draw_picture(picture, x, y, width, height):
glBindTexture(GL_TEXTURE_2D, textures[picture])
glPushMatrix()
glTranslatef(x, y, 0.0)
glBegin(GL_QUADS)
# Bottom Left Of The Texture and Quad:
glTexCoord2f(0.0, 0.0)
glVertex2f(0, 0)
# Bottom Right Of The Texture and Quad:
glTexCoord2f(1.0, 0.0)
glVertex2f(width, 0)
# Top Right Of The Texture and Quad:
glTexCoord2f(1.0, 1.0)
glVertex2f(width, height)
# Top Left Of The Texture and Quad:
glTexCoord2f(0.0, 1.0)
glVertex2f(0, height)
glEnd()
glPopMatrix()
def sigmoid(value):
return 1.0 / (1.0 + exp(-float(value)))
def sigmoid_0_1(value):
return sigmoid(value * 12.0 - 6.0)
def fake_sigmoid(value):
if value == 0.0 or value == 1.0:
return value
delta = sigmoid_0_1(1) - sigmoid_0_1(0)
res = sigmoid_0_1(value) / delta - sigmoid_0_1(0)
if res < 0.0:
return 0.0
elif res > 1.0:
return 1.0
else:
return res
def angle_difference(a1, a2):
difference = a1 - a2
if abs(difference) > 180:
difference = difference % 180
if a1 > a2:
difference = -difference
return difference
def display():
start_point = (
start_picture_coord[0] * HEIGHT * mosaic_factory.ratio / (args.tiles - 1),
start_picture_coord[1] * HEIGHT / (args.tiles - 1),
)
center = (HEIGHT * mosaic_factory.ratio / 2.0, HEIGHT / 2.0)
reverse_sigmoid_progress = fake_sigmoid(1 - progress)
sigmoid_progress = 1 - reverse_sigmoid_progress
max_zoom = args.tiles
zoom = max_zoom**reverse_sigmoid_progress
angle = start_orientation + sigmoid_progress * angle_difference(
current_mosaic_picture.orientation, start_orientation
)
if reverse_sigmoid_progress > 0.1:
alpha = 1.0
else:
alpha = reverse_sigmoid_progress * 10.0
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glTranslatef(center[0], center[1], 0.0)
glRotatef(angle, 0, 0, 1)
glTranslatef(-center[0], -center[1], 0.0)
glTranslatef(start_point[0], start_point[1], 0.0)
glScalef(zoom, zoom, 1.0)
glTranslatef(-start_point[0], -start_point[1], 0.0)
glColor4f(0.0, 0.0, 0.0, alpha)
glCallList(mosaic_display_lists[current_mosaic_picture])
glColor4f(0.0, 0.0, 0.0, 1.0 - alpha)
glScalef(max_zoom, max_zoom, 1.0)
glCallList(picture_display_lists[current_mosaic_picture])
glPopMatrix()
def spin_display():
global progress
global current_mosaic_picture
global start_picture_coord
global start_orientation
duration = args.duration
old_progress = progress
elapsed_time = sdl2.SDL_GetTicks() / 1000
progress = (elapsed_time % duration) / duration
if progress < old_progress:
current_tile_picture = current_mosaic_picture
start_orientation = current_mosaic_picture.orientation
current_mosaic_picture = iterator.__next__()
start_picture_coord = find_picture_in_mosaic(
current_tile_picture,
mosaic_factory.cached_mosaic(
current_mosaic_picture, args.tiles, args.reuse
),
)
def init():
width = mosaic_factory.ratio * size
height = size
print("loading textures:")
for i, img in enumerate(mosaic_factory.images.values()):
print(" {0}/{1}".format(i + 1, len(mosaic_factory.images)))
textures[img] = load_texture(img)
generate_picture_display_list(img, width, height)
print("generating mosaic display lists:")
for i, img in enumerate(mosaic_factory.images.values()):
print(" {0}/{1}".format(i + 1, len(mosaic_factory.images)))
generate_mosaic_display_list(img)
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glClearColor(0.0, 0.0, 0.0, 0.0)
glShadeModel(GL_FLAT)
def reshape(w, h):
glViewport(0, 0, w, h)
ratio = float(w) / h
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
viewport_center = HEIGHT * ratio / 2
photo_center = HEIGHT * mosaic_factory.ratio / 2
left = photo_center - viewport_center
right = photo_center + viewport_center
glOrtho(left, right, 0.0, HEIGHT, -1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def main():
global mosaic_factory
global size
global args
global progress
global textures
global picture_display_lists
global mosaic_display_lists
global start_picture_coord
global HEIGHT
global start_orientation
global current_mosaic_picture
global iterator
args = parser.parse_args()
progress = 0.0
textures = {}
picture_display_lists = {}
mosaic_display_lists = {}
mosaic_factory = MosaicFactory()
mosaic_factory.load(args.folder)
HEIGHT = 100.0
size = HEIGHT / args.tiles
iterator = image_iterator(mosaic_factory, args.tiles, args.reuse)
current_tile_picture = iterator.__next__()
current_mosaic_picture = iterator.__next__()
start_orientation = current_tile_picture.orientation
start_picture_coord = find_picture_in_mosaic(
current_tile_picture,
mosaic_factory.cached_mosaic(current_mosaic_picture, args.tiles, args.reuse),
)
sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
window = sdl2.SDL_CreateWindow(
"Mosaic for {}".format(args.folder).encode("utf8"),
sdl2.SDL_WINDOWPOS_CENTERED,
sdl2.SDL_WINDOWPOS_CENTERED,
640,
480,
sdl2.SDL_WINDOW_OPENGL | sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_RESIZABLE,
)
if not window:
sys.stderr.write("Error: Could not create window\n")
exit(1)
sdl2.SDL_GL_CreateContext(window)
init()
reshape(640, 480)
event = sdl2.SDL_Event()
running = True
while running:
while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
if event.type == sdl2.SDL_QUIT:
running = False
if (
event.type == sdl2.events.SDL_WINDOWEVENT
and event.window.event == sdl2.SDL_WINDOWEVENT_RESIZED
):
reshape(event.window.data1, event.window.data2)
display()
spin_display()
sdl2.SDL_GL_SwapWindow(window)
if __name__ == "__main__":
main()