forked from tdamdouni/Pythonista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgif-art-in-python.py
448 lines (333 loc) · 12.5 KB
/
gif-art-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
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
https://forum.omz-software.com/topic/2052/gif-art-in-python/13
import numpy as np
import gizeh
import moviepy.editor as mpy
W,H = 128,128
duration = 2
ncircles = 20 # Number of circles
def make_frame(t):
surface = gizeh.Surface(W,H)
for i in range(ncircles):
angle = 2*np.pi*(1.0*i/ncircles+t/duration)
center = W*( 0.5+ gizeh.polar2cart(0.1,angle))
circle = gizeh.circle(r= W*(1.0-1.0*i/ncircles),
xy= center, fill= (i%2,i%2,i%2))
circle.draw(surface)
return surface.get_npimage()
clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_gif("circles.gif",fps=15, opt="OptimizePlus", fuzz=10)
#==============================
# coding: utf-8
import numpy as np
from PIL import Image, ImageDraw
from images2gif import writeGif
from math import sin,cos
W,H = 128,128
duration = 2
ncircles = 20 # Number of circles
def polar2cart(r,theta):
x = r*cos(theta)
y = r*sin(theta)
return x, y
def make_frame(t):
im = Image.new('RGB', (W,H), (0,0,0))
surface = ImageDraw.Draw(im)
for i in range(ncircles):
angle = 2*np.pi*(1.0*i/ncircles+t/duration)
center = W*( 0.5 + polar2cart(0.1,angle)[0]), W*( 0.5 + polar2cart(0.1,angle)[1])
r = W*(1.0-1.0*i/ncircles)
bbox = (center[0]-r, center[1]-r, center[0]+r, center[1]+r)
surface.ellipse(bbox, fill= (i%2*255,i%2*255,i%2*255))
del surface
return im
images = []
for x in range(200):
images.append(make_frame(x/25.0))
writeGif('tunnelswirl.gif',images,0.005)
#==============================
images = [make_frame(x/25.0) for x in xrange(200)] # ;-)
#==============================
images.append(make_frame(x/25.0).convert('RGB'))
#==============================
# coding: utf-8
import numpy as np
from PIL import Image, ImageDraw
from images2gif import writeGif
from math import sin,cos
W,H = 1024,1024
duration = 2
ncircles = 20 # Number of circles
def polar2cart(r,theta):
x = r*cos(theta)
y = r*sin(theta)
return x, y
def make_frame(t):
im = Image.new('RGB', (W,H), (0,0,0))
surface = ImageDraw.Draw(im)
for i in range(ncircles):
angle = 2*np.pi*(1.0*i/ncircles+t/duration)
center = W*( 0.5 + polar2cart(0.1,angle)[0]), W*( 0.5 + polar2cart(0.1,angle)[1])
r = W*(1.0-1.0*i/ncircles)
bbox = (center[0]-r, center[1]-r, center[0]+r, center[1]+r)
surface.ellipse(bbox, fill= (i%2*255,i%2*255,i%2*255))
del surface
return im.resize((256,256), Image.ANTIALIAS)
images = []
for x in range(50):
images.append(make_frame(x/25.0))
writeGif('tunnelswirl.gif',images,0.005)
#==============================
# coding: utf-8
import colorsys, console
import numpy as np
from images2gif import writeGif
from PIL import Image, ImageDraw
sin, cos, pi = np.sin, np.cos, np.pi
W,H = 1024,1024
NFACES = 5 #Number of faces on the polygon
R = 0.3 #Radius of polygon
NSQUARES = 100 # Number of squares
DURATION = 1
def polar_polygon(nfaces,radius, npoints):
""" Returns the (x,y) coordinates of n points regularly spaced
along a regular polygon of `nfaces` faces and given radius.
"""
theta=np.linspace(0,2*np.pi,npoints)[:-1]
n = nfaces
r= cos( pi/n )/cos((theta%(2*pi/n))-pi/n)
d = np.cumsum(np.sqrt(((r[1:]-r[:-1])**2)))
d = [0]+list(d/d.max())
return zip(radius*r, theta, d)
def polar2cart(r,theta):
x = r*cos(theta)
y = r*sin(theta)
return x, y
def squarecoords(sidelength, center, angle):
cx, cy = center
radius = sidelength/2
corners = [(cx-radius,cy-radius), (cx+radius,cy-radius), (cx+radius,cy+radius), (cx-radius,cy+radius)]
def rotate(point, angle, center=(0, 0)):
theta = angle*(np.pi/180.0)
translated = point[0]-center[0] , point[1]-center[1]
rotated = (translated[0]*cos(theta)-translated[1]*sin(theta),translated[0]*sin(theta)+translated[1]*cos(theta))
newcoords = (round(rotated[0]+center[0], 1),round(rotated[1]+center[1], 1))
return newcoords
newcorners = []
for x in corners:
newcorners.append(rotate(x,angle,center))
return tuple([tuple([int(x) for x in y]) for y in newcorners])
def half(t, side="left"):
points = polar_polygon(NFACES, R, NSQUARES)
ipoint = 0 if side=="left" else NSQUARES/2
points = (points[ipoint:]+points[:ipoint])[::-1]
i = Image.new('RGB', (W, H), (0,0,0))
surface = ImageDraw.Draw(i)
for (r, th, d) in points:
center = W*(0.5+polar2cart(r,th)[0]),W*(0.5+polar2cart(r,th)[1])
#angle = -(np.pi*d + t*np.pi/DURATION)*50
angle = -(t*180)
color= colorsys.hls_to_rgb((2*d+t/DURATION)%1,.5,.5)
color = tuple([int(x*255) for x in color])
coords = squarecoords(0.17*W, center, angle)
surface.polygon(coords, color, outline=(255,255,255))
im = np.asarray(i)
return (im[:,:W/2] if (side=="left") else im[:,W/2:])
def make_frame(t):
lefthalf = half(t,"left")
righthalf = half(t,"right")
return Image.fromarray(np.hstack((lefthalf, righthalf)))
images = []
for x in range(100):
images.append(make_frame(x/100.0).resize((512,512), Image.ANTIALIAS))
console.clear()
print str(x+1)+'%'
console.clear()
print 'Writing gif...'
writeGif("pentagon.gif", images, duration=0.01)
#==============================
# coding: utf-8
import math
from operator import itemgetter
import console
from images2gif import writeGif
from PIL import Image, ImageDraw
W, H = (1024, 1024)
class Point3D:
def __init__(self, x = 0, y = 0, z = 0):
self.x, self.y, self.z = float(x), float(y), float(z)
def rotateX(self, angle):
""" Rotates the point around the X axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
y = self.y * cosa - self.z * sina
z = self.y * sina + self.z * cosa
return Point3D(self.x, y, z)
def rotateY(self, angle):
""" Rotates the point around the Y axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
z = self.z * cosa - self.x * sina
x = self.z * sina + self.x * cosa
return Point3D(x, self.y, z)
def rotateZ(self, angle):
""" Rotates the point around the Z axis by the given angle in degrees. """
rad = angle * math.pi / 180
cosa = math.cos(rad)
sina = math.sin(rad)
x = self.x * cosa - self.y * sina
y = self.x * sina + self.y * cosa
return Point3D(x, y, self.z)
def project(self, win_width, win_height, fov, viewer_distance):
""" Transforms this 3D point to 2D using a perspective projection. """
factor = fov / (viewer_distance + self.z)
x = self.x * factor + win_width / 2
y = -self.y * factor + win_height / 2
return Point3D(x, y, self.z)
class Cube:
def __init__(self, win_width = 640, win_height = 480):
self.vertices = [
Point3D(-1,1,-1),
Point3D(1,1,-1),
Point3D(1,-1,-1),
Point3D(-1,-1,-1),
Point3D(-1,1,1),
Point3D(1,1,1),
Point3D(1,-1,1),
Point3D(-1,-1,1)
]
# Define the vertices that compose each of the 6 faces. These numbers are
# indices to the vertices list defined above.
self.faces = [(0,1,2,3),(1,5,6,2),(5,4,7,6),(4,0,3,7),(0,4,5,1),(3,2,6,7)]
# Define colors for each face
self.colors = [
'#FFEC94',
'#FFAEAE',
'#404040',
'#B0E57C',
'#B4D8E7',
'#7BC8A4'
]
def make_frame(self, angle):
# It will hold transformed vertices.
t = []
screen = Image.new('RGB',(W, H), (255,255,255))
draw = ImageDraw.Draw(screen)
for v in self.vertices:
# Rotate the point around X axis, then around Y axis, and finally around Z axis.
r = v.rotateX(angle).rotateY(angle).rotateZ(angle)
# Transform the point from 3D to 2D
#if angle <= 180:
# p = r.project(W, H, 256, 3+(angle/90.0))
#else:
# p = r.project(W, H, 256, 5-((angle-180)/90.0))
p = r.project(W, H, 256, 3)
# Put the point in the list of transformed vertices
t.append(p)
# Calculate the average Z values of each face.
avg_z = []
i = 0
for f in self.faces:
z = (t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z) / 4.0
avg_z.append([i,z])
i = i + 1
# Draw the faces using the Painter's algorithm:
# Distant faces are drawn before the closer ones.
for tmp in sorted(avg_z,key=itemgetter(1),reverse=True):
face_index = tmp[0]
f = self.faces[face_index]
pointlist = [(t[f[0]].x, t[f[0]].y), (t[f[1]].x, t[f[1]].y),
(t[f[1]].x, t[f[1]].y), (t[f[2]].x, t[f[2]].y),
(t[f[2]].x, t[f[2]].y), (t[f[3]].x, t[f[3]].y),
(t[f[3]].x, t[f[3]].y), (t[f[0]].x, t[f[0]].y)]
draw.polygon(pointlist, fill=self.colors[face_index])
return screen.resize((512,512), Image.ANTIALIAS)
c = Cube()
images = []
for x in range(0,360,5):
images.append(c.make_frame(x))
console.clear()
print str((x/360.0)*100.0)[:5]+'%'
writeGif('cube.gif', images, 0.01)
#==============================
Duration = 1
#==============================
Duration = 1.0
#==============================
# coding: utf-8
from images2gif import writeGif
from PIL import Image, ImageDraw, ImageChops
import console
W, H = 1024,1024
RESOLUTION = 5
FG, BG = '#ffbb00', '#009bff'
#FG, BG = '#000000', '#ffffff'
def drange(start, stop, step=1):
n = int(round((stop - start)/float(step)))
if n > 1:
return([start + step*i for i in range(n+1)])
else:
return([])
def irange(start, increments):
mylist = [start]
for i in increments:
mylist.append(mylist[-1]+i)
return mylist
class Rect:
def __init__(self, left, top, width, height):
self.left = left
self.right = left + width
self.top = top
self.bottom = top+height
self.centerx = left + (width/2)
self.centery = top + (height/2)
self.width = width
self.height = height
self.bbox = (left, top), (self.right, self.bottom)
def drawSierpinski(surf, rect, fgcolor=FG, bgcolor=BG, level=6, topshade=True):
try:
rect.left
except AttributeError:
left, top, width, height = rect
rect = Rect(left, top, width, height)
if level == 0:
return
quarterWidth = (rect.width/4)+rect.left
threeQuarterWidth = (rect.width/4*3)+rect.left
topRect = Rect(quarterWidth, rect.top, (rect.width / 2), (rect.height / 2))
leftRect = Rect(rect.left, rect.centery, (rect.width/2), (rect.height/2))
rightRect = Rect(rect.centerx, rect.centery, (rect.width / 2), (rect.height / 2))
#Shade topleft
if topshade:
surf.rectangle((rect.left, rect.top, rect.centerx, rect.bottom), fill=bgcolor)
#outer triangle
surf.polygon([(rect.centerx,rect.top),(rect.left,rect.bottom),(rect.right,rect.bottom)],fgcolor)
#inner upside-down triangle
surf.polygon([(quarterWidth,rect.centery),(rect.centerx,rect.bottom),(threeQuarterWidth, rect.centery)],bgcolor)
#do recursive calls
drawSierpinski(surf, topRect, fgcolor, bgcolor, level-1, topshade)
drawSierpinski(surf, leftRect, fgcolor, bgcolor, level-1, topshade)
drawSierpinski(surf, rightRect, fgcolor, bgcolor, level-1, topshade)
return im
def make_sierpinski(level, topshade=True):
im = Image.new('RGB', (W, H), (255,255,255))
surf = ImageDraw.Draw(im)
drawSierpinski(surf, Rect(0, 0, W, H), FG, BG, level, topshade)
return im
im = Image.new('RGB', (W, H), (255,255,255))
surf = ImageDraw.Draw(im)
sierpinski1 = make_sierpinski(RESOLUTION)
sierpinski2 = make_sierpinski(RESOLUTION+1)
numbers = [int(n) for n in irange(0, drange(8,4,-0.0475))]
images = []
for x in numbers:
a = sierpinski1.crop((x, x, W, W)).resize((512, 512), Image.ANTIALIAS)
b = sierpinski2.crop((x, x, W, W)).resize((512, 512), Image.ANTIALIAS)
alpha = x / float(W/2)
images.append(Image.blend(a, b, alpha))
console.clear()
print str(alpha*100)+'%'
console.clear()
print 'writing...'
writeGif('sierpinski.gif', images, 2.0/len(images))