Spaces:
Runtime error
Runtime error
File size: 2,553 Bytes
f265950 |
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 |
import os
import imageio
import numpy as np
from glob import glob
from tqdm import tqdm
from PIL import Image, ImageDraw, ImageFont
def create_image(size, text, font):
W, H = size
image = Image.new('L', size, 255)
draw = ImageDraw.Draw(image)
_, _, w, h = draw.textbbox((0, 0), text, font=font)
draw.text(((W-w)/2, (H-h)/2), text, font=font, fill=0)
return image
if __name__ == "__main__":
languages = ['arabic', 'greek', 'chinese', 'russian', 'tamil', 'english']
scripts = ['arabic', 'greek', 'chinese', 'cyrillic', 'tamil', 'latin']
concepts = ['panda', 'car', 'music', 'bird', 'star', 'cloud']
width, height = 160, 160
n_frames = 67
freeze = 5
text_height = 40
nx, ny = len(concepts), len(scripts)
collage = np.ones((n_frames*2+freeze-1, text_height+width*nx, height*ny)).astype(np.uint8)*255
savepath = "../images/languages.gif"
dirpath = "../examples/concepts"
font = ImageFont.truetype('data/fonts/latin/Roboto-Regular.ttf', 28)
for i in tqdm(range(ny)):
background = create_image((width, text_height), languages[i].capitalize(), font)
lang_image = np.asarray(background)
for idx in range(n_frames*2+freeze-1):
collage[idx, :text_height, i*width:(i+1)*width] = lang_image
for i, concept in tqdm(enumerate(concepts), total=len(concepts)):
for j, script in enumerate(scripts):
filepath = os.path.join(dirpath, concept, f"{script}.gif")
image = Image.open(filepath)
assert image.is_animated
image.seek(0)
frame = image.convert('L').copy()
frame = frame.resize((width,height))
for idx in range(freeze):
collage[idx, text_height+i*width:text_height+(i+1)*width,j*height:(j+1)*height] = np.asarray(frame)
for frame_idx in range(n_frames):
image.seek(frame_idx)
frame = image.convert('L').copy()
frame = frame.resize((width,height))
collage[idx, text_height+i*width:text_height+(i+1)*width,j*height:(j+1)*height] = np.asarray(frame)
idx += 1
for frame_idx in reversed(range(n_frames)):
image.seek(frame_idx)
frame = image.convert('L').copy()
frame = frame.resize((width,height))
collage[idx, text_height+i*width:text_height+(i+1)*width,j*height:(j+1)*height] = np.asarray(frame)
idx += 1
imageio.mimsave(savepath, collage) |