Spaces:
Runtime error
Runtime error
File size: 1,421 Bytes
913d3e3 |
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 |
import os
import imageio
import numpy as np
from glob import glob
from PIL import Image, ImageSequence
if __name__ == "__main__":
path = "/Users/bkhmsi/Desktop/Animal-Words/*.gif"
save_path = os.path.join(os.path.dirname(path), "collage.gif")
width, height = 400, 400
nx, ny = 5, 5
n_frames = 67
collage = np.ones((n_frames+10, width*nx, height*ny)).astype(np.uint8)
filenames = [p for p in glob(path) if os.path.basename(p)[:-4] not in ["palestine", "amin", "collage"]]
print(f"> {len(filenames)} Files Found")
for file in filenames:
print(os.path.basename(file))
assert nx*ny <= len(filenames)
for i in range(nx):
for j in range(ny):
image = Image.open(filenames[i*ny+j])
assert image.is_animated
idx = 0
for frame_idx in range(image.n_frames):
image.seek(frame_idx)
frame = image.convert('L').copy()
if frame_idx == 0 or frame_idx == image.n_frames-1:
for _ in range(5):
collage[idx, i*width:(i+1)*width,j*height:(j+1)*height] = np.asarray(frame)[100:500, 100:500]
idx += 1
else:
collage[idx, i*width:(i+1)*width,j*height:(j+1)*height] = np.asarray(frame)[100:500, 100:500]
idx += 1
imageio.mimsave(save_path, collage)
|