File size: 1,828 Bytes
c865888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import textwrap
from PIL import Image, ImageDraw, ImageFont
import imageio
import os

# convert the numerical labels tok characters
def convert_num_to_char(
        tokens: list,
        char_tokens: any
    ) -> str:
    return "".join([tokens[num] for num in char_tokens.tolist()])

# draw text onto white page
def draw_text(
        image: any,
        text: any,
        font: any,
        position: tuple=(0,0),
        max_width: any=None,
        fill: tuple=(0,0,0)
    ) -> None:

    draw = ImageDraw.Draw(image)
    if max_width:
        wrapped_text = textwrap.fill(text, width=max_width)
    else:
        wrapped_text = text
    draw.multiline_text(position, wrapped_text, font=font, fill=fill)


# create gif animation 
def generate_text_animation(
        text_list: list,
        text_animation_path: str,
        output_temp_path: str='./outputs/temp_files'
    ) -> None:

    # create images with text
    image_files = []
    for index, text in enumerate(text_list):
        
        img = Image.new('RGB', (600, 159), color=(255, 255, 255))  # Create a white image
        font = ImageFont.load_default()
        draw_text(img, text, font, position=(10, 10), max_width=80, fill=(0, 0, 0))

        # Save image to a temporary file
        os.makedirs(output_temp_path, exist_ok=True)
        # temp_file = f'./outputs/temp_image_{index}.png'
        temp_file = output_temp_path + f'/temp_image_{index}.png'
        img.save(temp_file)
        image_files.append(temp_file)

    # Read saved images and create a GIF
    images = [imageio.imread(file) for file in image_files]
    imageio.mimsave(
            text_animation_path,
            images,
            format='GIF',
            duration=0.2,
    )

    # clean up temp image files
    for file in image_files:
        os.remove(file)
    return