-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjulio_le_parc_replications.py
644 lines (540 loc) · 22.8 KB
/
julio_le_parc_replications.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
"""Replication of, and variations on, works from 1959 by Julio Le Parc.
Namely, the pieces studied are as referenced below (all references are from
The Metropolitan Museum of Art):
* Mutation of Forms (1959):
https://www.metmuseum.org/art/collection/search/815337
* Rotations (1959):
https://www.metmuseum.org/art/collection/search/815346
* Rotation of Fractioned Circles (1959):
https://www.metmuseum.org/art/collection/search/815341
* Rotation in Red and Black (1959):
https://www.metmuseum.org/art/collection/search/815338
See also Le Parc's website for information about the artist of the original
works:
http://www.julioleparc.org/
"""
from abc import ABCMeta, abstractmethod
import itertools
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.transforms as mtransforms
import numpy as np
IMAGE_PAD_POINTS = 2
class LeParcDesign(metaclass=ABCMeta):
"""Abstract Base Class to replicate a 1959 Julio Le Parc design.
For more detail on the artist Julio Le Parc, see:
http://www.julioleparc.org/
"""
def __init__(self, design_name, gridpoints, colours):
"""Set up a new 1959 Julio Le Parc design instance."""
self.design_name = design_name
self.gridpoints = gridpoints
self.grid_indices = range(self.gridpoints)
self.colours = colours
self.background_colour = None
fig, ax = plt.subplots(figsize=(6, 6))
self.fig = fig
self.axes = ax
self.fig.set_canvas(plt.gcf().canvas)
self.format_plt()
self.patches = None
self.save_to_dir = self.design_name.lower().replace(" ", "_")
self.angles_array = self.create_design_angles_array()
self.ABC_error_msg = "Designs must be created by subclassing."
@abstractmethod
def create_design_patches_per_gridpoint(self):
"""Create the underlying patches to rotate at each gridpoint."""
raise NotImplementedError(self.ABC_error_msg)
@abstractmethod
def create_design_angles_array(self):
"""Create the array of patch rotation angles per gridpoint."""
raise NotImplementedError(self.ABC_error_msg)
@abstractmethod
def create_design(self):
"""Create a design by placing and rotating relevant patches."""
raise NotImplementedError(self.ABC_error_msg)
def format_canvas(self):
"""Format canvas to centre and remove any axes markings."""
self.background_colour = self.colours["OFF WHITE"]
self.fig.patch.set_facecolor(self.background_colour)
padding_per_side = 2
limits = (
self.gridpoints - padding_per_side,
self.gridpoints + padding_per_side,
)
self.axes.set_xlim(*limits)
self.axes.set_ylim(*limits)
def format_plt(self):
"""Format the plot to add padding and hide axes components."""
# Note: use this instead of plt.axis('equal') since due to the
# rotation of outer patches in the animation, 'equal' will vary
# somewhat and therefore the whole animation will shift and re-size
# slightly otherwise, but want all patches fixed in position.
min_point = 1
max_point = self.gridpoints + 2 # +2 to pad by 1 on each side
plt.axis([min_point, max_point, min_point, max_point])
plt.axis("off")
plt.xticks([])
plt.yticks([])
def plot_and_save_design(self):
"""Plot and save a static replication of a Le Parc design."""
self.format_canvas()
self.create_design()
self.format_plt()
plt.tight_layout()
# For creating unique names to save generated variations whilst trying
# to get the angles the same as the original(!):
# # import uuid
plt.savefig(
"img/{}/replication_of_original.png".format(self.save_to_dir),
# # 'img/rotation_in_red_and_black/misc_variations/'
# # '{}.png'.format(uuid.uuid4()),
format="png",
bbox_inches="tight",
facecolor=self.background_colour,
)
def init_animated_design(self):
"""Initiate animation of a Julio Le Parc 1959 design."""
self.create_design()
self.format_plt()
def update_animation_for_uniform_rotation(self, i):
"""Update the animation frame to rotate the design uniformly."""
# !!!
# TODO: this code as-is is not at all efficient since it is
# re-generating the same patches, but at different collective
# rotations, for every single frame. Instead retain the patches
# created the first time and continuously update here the rotation
# only via setting the relevant properties on those.
# !!!
self.axes.cla()
self.angles_array = (
self.create_design_angles_array() + 10 * i * np.pi / 12
)
self.create_design()
self.format_plt()
def plot_and_save_animated_design(self):
"""Plot and save an animated 1959 Le Parc design replication."""
self.format_canvas()
# Note: increase *frames* for longer video, decrease *interval* and/or
# addition to angles_array to make the video smoother (less choppy)
anim = animation.FuncAnimation(
self.fig,
self.update_animation_for_uniform_rotation,
init_func=self.init_animated_design,
interval=5,
frames=240,
)
plt.tight_layout()
# Note (see also above comment): increase *fps* to speed up the video
anim.save(
"img/{}/animation_with_uniform_rotation.mp4".format(
self.save_to_dir
),
writer=animation.FFMpegWriter(
fps=30, extra_args=["-vcodec", "libx264"]
),
)
class Mutations(LeParcDesign):
"""Replicate and animate Le Parc's 'Mutation of Forms'.
For more detail on the original piece, see:
https://www.metmuseum.org/art/collection/search/815337
"""
def __init__(self):
"""Set up a new 'Mutation of Forms' replication instance."""
super().__init__(
"MUTATION OF FORMS",
10,
{
"OFF WHITE": "#FAEFDD",
"RED": "#CB0B22",
"BLUE": "#1D119B",
},
)
self.red_angles_array = self.angles_array # from parent LeParcDesign
self.blue_angles_array = self.create_design_angles_array(is_red=False)
@staticmethod
def plot_mutations_wedge(centre, theta1, theta2, colour):
"""Create a single wedge patch with given angular coverage."""
# 0.5 radius means the circles containing the wedges just touch their
# neighbours. Use 0.475 to provide a small gap as per the design.
return mpatches.Wedge(centre, 0.475, theta1, theta2, color=colour)
def create_design_patches_per_gridpoint(
self, position, wedge_1_thetas, wedge_2_thetas, colour_1, colour_2
):
"""Create the underlying patches to rotate at each gridpoint."""
wedge_1 = self.plot_mutations_wedge(
position, *wedge_1_thetas, colour_1
)
wedge_2 = self.plot_mutations_wedge(
position, *wedge_2_thetas, colour_2
)
return wedge_1, wedge_2
def create_mutations_linspaced_angles(self, max_coverage, min_coverage):
"""Create a 1D array of evenly-spaced wedge coverage angles.
The even spacing between the leftmost and rightmost angles for the
start, and separately, end, wedge angles is achieved using the NumPy
function 'linspace'.
Note: angles start pointing downwards i.e. 0 degs is south in PyPlot.
So red wedges are constrained to -135 to +45, blues to +45 to +225.
Original design angular coverage pattern is:
max blue ............. min blue
min red ............. max red
.. .. ..
.. .. ..
...........half blue...........
...........half red............
.. .. ..
.. .. ..
min blue ............. max blue
max red ............. min red
Define max and min angular coverages for the wedges:
* red wedges go from -135 <- -45 -> +45
* blue wedges go from +45 <- +135 -> +225
"""
theta1_min_to_max = np.linspace(
max_coverage[0], min_coverage[0], num=self.gridpoints
)
theta2_min_to_max = np.linspace(
max_coverage[1], min_coverage[1], num=self.gridpoints
)
return np.column_stack((theta1_min_to_max, theta2_min_to_max))
def create_design_angles_array(self, is_red=True):
"""Create the array of patch rotation angles per gridpoint."""
red_max = (-135, 45)
red_min = (-45, -45)
blue_max = (45, 225)
blue_min = (135, 135)
angles_array = np.zeros(
(self.gridpoints, self.gridpoints), dtype=(float, 2)
)
if is_red:
index = 1 # don't change the spaced_thetas array later (c.f. -1)
spaced_thetas = self.create_mutations_linspaced_angles(
max_coverage=red_max, min_coverage=red_min
)
else:
index = -1 # to reverse the spaced_thetas array later, via [::-1]
spaced_thetas = self.create_mutations_linspaced_angles(
max_coverage=blue_max, min_coverage=blue_min
)
# 1. Make first and last column correct:
for j in self.grid_indices:
angles_array[0][j] = spaced_thetas[::index][j]
angles_array[-1][j] = spaced_thetas[::index][-j - 1]
# 2. Create rows linearly-spaced based on first and last columns:
for i in self.grid_indices:
row_angles = self.create_mutations_linspaced_angles(
max_coverage=angles_array[0][i],
min_coverage=angles_array[-1][i],
)
angles_array[i] = row_angles
return angles_array
def create_design(self):
"""Create a design by placing and rotating relevant patches."""
for i, j in itertools.product(self.grid_indices, self.grid_indices):
# Get angles:
red_thetas = self.red_angles_array[i][j]
blue_thetas = self.blue_angles_array[i][j]
# Now create and plot the wedges onto the canvas:
position_xy = (IMAGE_PAD_POINTS + i, IMAGE_PAD_POINTS + j)
red_wedge, blue_wedge = self.create_design_patches_per_gridpoint(
position_xy,
red_thetas,
blue_thetas,
colour_1=self.colours["RED"],
colour_2=self.colours["BLUE"],
)
self.axes.add_patch(red_wedge)
self.axes.add_patch(blue_wedge)
def update_animation_for_uniform_rotation(self, i):
"""Update the animation frame to rotate the design uniformly."""
self.axes.cla()
# In this case there are two angles (red and blue) that need updating
angle_addition_per_frame = 10 * i * np.pi / 12
self.red_angles_array = (
self.create_design_angles_array() + angle_addition_per_frame
)
self.blue_angles_array = (
self.create_design_angles_array(is_red=False)
+ angle_addition_per_frame
)
self.create_design()
self.format_plt()
class Rotations(LeParcDesign):
"""Replicate and animate Le Parc's 'Rotations'.
For more detail on the original piece, see:
https://www.metmuseum.org/art/collection/search/815346
"""
def __init__(self):
"""Set up a new 'Rotations' replication instance."""
super().__init__(
"ROTATIONS",
13,
{
"OFF WHITE": "#F4EDE5",
"OFF BLACK": "#161815",
},
)
def create_design_patches_per_gridpoint(
self, centre, rect_angle, foreground_colour, background_colour
):
"""Create the underlying patches to rotate at each gridpoint."""
# These parameters are adapted to match the original design:
radius = 0.45
offset_amount = 0.3
padding = 0.03
# Note: get a very thin but still visible edge to circle even if set
# linewidth to zero, so (workaround) make edgecolour background colour.
patch = mpatches.Circle(
centre,
radius,
facecolor=foreground_colour,
edgecolor=background_colour,
)
# The clipping rectangle, rotated appropriately.
clip_patch = mpatches.Rectangle(
(centre[0] + offset_amount, centre[1] - radius),
radius - offset_amount + padding,
2 * radius,
color=background_colour,
transform=mtransforms.Affine2D().rotate_deg_around(
*centre, rect_angle
)
+ self.axes.transData,
)
return (patch, clip_patch)
def create_design_angles_array(self):
"""Create the array of patch rotation angles per gridpoint."""
angles_array = np.zeros(
(self.gridpoints, self.gridpoints), dtype=float
)
spaced_thetas = np.linspace(0, 180, self.gridpoints)
# 1. Make first and last column correct:
for j in self.grid_indices:
angles_array[0][j] = spaced_thetas[j]
angles_array[-1][j] = spaced_thetas[-j - 1]
# 2. Create rows linearly-spaced based on first and last columns:
for i in self.grid_indices:
# Minus sign is to achieve clockwise angle changes when going from
# the first to the last item in the array, as per the design.
row_angles = np.linspace(
-1 * angles_array[0][i], # see above regarding -1 factor
angles_array[-1][i],
self.gridpoints,
)
angles_array[i] = row_angles
return angles_array
def create_design(self, angles_array=None):
"""Create a design by placing and rotating relevant patches."""
if angles_array is None:
angles_array = self.angles_array
for i, j in itertools.product(self.grid_indices, self.grid_indices):
position_xy = (IMAGE_PAD_POINTS + i, IMAGE_PAD_POINTS + j)
circle, clip_rectangle = self.create_design_patches_per_gridpoint(
position_xy,
self.angles_array[i][j],
self.colours["OFF BLACK"],
self.colours["OFF WHITE"],
)
self.axes.add_patch(circle)
clip_rectangle.set_clip_path(circle)
self.axes.add_patch(clip_rectangle)
class Fractioned(LeParcDesign):
"""Replicate and animate Le Parc's 'Rotation of Fractioned Circles'.
For more detail on the original piece, see:
https://www.metmuseum.org/art/collection/search/815341
"""
def __init__(self):
"""Set up a new 'Rotation of Fractioned Circles' replication."""
super().__init__(
"ROTATION OF FRACTIONED CIRCLES",
9,
{
"OFF WHITE": "#F5EFE3",
"LIGHT GREY": "#D3D2D0",
"DARK GREY": "#63676B",
},
)
def create_design_patches_per_gridpoint(
self, centre, rect_angle, dark_colour, light_colour, background_colour
):
"""Create the underlying patches to rotate at each gridpoint."""
# These parameters are adapted to match the original design:
radius = 0.45
offset_amount = 0.02
line_size = 0.12
# Note: get a very thin but still visible edge to circle even if set
# linewidth to zero, so (workaround) make edgecolour background colour.
light_patch = mpatches.Circle(
centre,
radius,
facecolor=light_colour,
edgecolor=background_colour,
)
start_at = (centre[0] + offset_amount, centre[1] - radius)
clip_alpha = 3
# The clipping rectangle, rotated appropriately.
clip_patch = mpatches.Rectangle(
start_at,
line_size,
2 * radius,
color=background_colour,
transform=mtransforms.Affine2D().rotate_deg_around(
*centre, rect_angle
)
+ self.axes.transData,
alpha=clip_alpha,
)
dark_patch = mpatches.Rectangle(
start_at,
radius,
2 * radius,
color=dark_colour,
transform=mtransforms.Affine2D().rotate_deg_around(
*centre, rect_angle
)
+ self.axes.transData,
alpha=clip_alpha - 1,
)
return (dark_patch, light_patch, clip_patch)
def create_design_angles_array(self):
"""Create the array of patch rotation angles per gridpoint."""
angles_array = np.zeros(
(self.gridpoints, self.gridpoints), dtype=float
)
first_col_thetas = np.linspace(-70, 70, self.gridpoints)
last_col_thetas = np.linspace(70, 3 * 360 + 290, self.gridpoints)
# 1. Make first and last column correct:
for j in self.grid_indices:
angles_array[0][j] = first_col_thetas[j]
angles_array[-1][j] = last_col_thetas[j]
# 2. Create rows linearly-spaced based on first and last columns:
for i in self.grid_indices:
row_angles = np.linspace(
angles_array[0][i],
angles_array[-1][i],
self.gridpoints,
)
angles_array[i] = row_angles
return -1 * np.flip(angles_array, axis=1)
def create_design(self, angles_array=None):
"""Create a design by placing and rotating relevant patches."""
if angles_array is None:
angles_array = self.angles_array
for i, j in itertools.product(self.grid_indices, self.grid_indices):
position_xy = (IMAGE_PAD_POINTS + i, IMAGE_PAD_POINTS + j)
design_patches = self.create_design_patches_per_gridpoint(
position_xy,
self.angles_array[i][j],
self.colours["DARK GREY"],
self.colours["LIGHT GREY"],
self.colours["OFF WHITE"],
)
dark_cir, light_cir, off_white_line = design_patches
self.axes.add_patch(light_cir)
dark_cir.set_clip_path(light_cir)
self.axes.add_patch(dark_cir)
off_white_line.set_clip_path(light_cir)
self.axes.add_patch(off_white_line)
class RedAndBlack(LeParcDesign):
"""Replicate and animate Le Parc's 'Rotation in Red and Black'.
For more detail on the original piece, see:
https://www.metmuseum.org/art/collection/search/815338
"""
def __init__(self):
"""Set up a new 'Rotation in Red and Black' replication."""
super().__init__(
"ROTATION IN RED AND BLACK",
10,
{
"OFF WHITE": "#F2ECE0",
"OFF BLACK": "#100F0D",
"RED": "#983134",
},
)
def create_design_patches_per_gridpoint(
self, centre, base_theta, colour_1, colour_2
):
"""Create the underlying patches to rotate at each gridpoint."""
half_length = 0.5
width = 0.05
# Define two lines perpendicular to each other as patches
reference_zorder = 1
cross_lines = self.create_cross_line(
centre, half_length, width, colour_1, base_theta, reference_zorder
) + self.create_cross_line(
centre,
half_length,
width,
colour_2,
base_theta + 90,
reference_zorder - 10, # i.e. this line is shown on top
)
return cross_lines
def create_cross_line(self, centre, length, width, colour, angle, zorder):
"""Create a single angled line patch to form half a cross."""
lines = []
for theta in (angle, angle + 180): # parallel half-lines from centre
# Centre is normalised with respect to the gridpoint
lines.append(
mpatches.Rectangle(
(centre[0] - width, centre[1] - (width / 2.0)), # as above
length,
width,
color=colour,
transform=mtransforms.Affine2D().rotate_deg_around(
*centre, theta
)
+ self.axes.transData,
zorder=zorder,
)
)
return lines
def create_design_angles_array(self):
"""Create the array of patch rotation angles per gridpoint."""
angles_array = np.zeros(
(self.gridpoints, self.gridpoints), dtype=float
)
# Alternate between +45 and -45 but with a different start point
first_col_thetas = np.full((self.gridpoints), 45)
first_col_thetas[1::2] = -45 # starts with 45 (then -45 is next, etc.)
last_col_thetas = np.full((self.gridpoints), 45)
last_col_thetas[::2] = -45 # starts with -45
# 1. Make first and last column correct:
angles_array[0] = first_col_thetas
angles_array[-1] = last_col_thetas
# 2. Create rows linearly-spaced based on first and last columns,
# where a cycle factor sets how many rotations from angles A to B.
for i in self.grid_indices:
use_cycle_factor = (i // 2) + 7
normalised_angles_a = angles_array[0][i]
normalised_angles_b = angles_array[-1][i] + 360 * use_cycle_factor
row_angles = np.linspace(
normalised_angles_a,
normalised_angles_b,
self.gridpoints,
)
angles_array[:, i] = row_angles
return angles_array
def create_design(self, angles_array=None):
"""Create a design by placing and rotating relevant patches."""
if angles_array is None:
angles_array = self.angles_array
for i, j in itertools.product(self.grid_indices, self.grid_indices):
# Now create and plot the wedges onto the canvas:
position_xy = (IMAGE_PAD_POINTS + i, IMAGE_PAD_POINTS + j)
line_patches = self.create_design_patches_per_gridpoint(
position_xy,
self.angles_array[i][j],
self.colours["RED"],
self.colours["OFF BLACK"],
)
for line in line_patches:
self.axes.add_patch(line)
# Plot and show all four designs as both the static originals and as
# animated videos where the patches all rotate uniformly:
for design_class in [Mutations, Rotations, Fractioned, RedAndBlack]:
design_class().plot_and_save_design()
design_class().plot_and_save_animated_design()
plt.show()