Skip to content

Web#

This example illustrates how to animate a GIF image, from the web, in text art.

Try it yourself!

¡Apagando las luces!

Source

 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
#!/usr/bin/env python
# -*- coding: utf-8 name> -*-

"""This script animates a GIF image, from the web, in text art."""

from io import BytesIO
import time

from PIL import Image
from picharsso import new_drawer
from picharsso.utils import clear_screen, terminal_size

import requests


if __name__ == "__main__":
    # Set URL of image
    image_url = "https://bit.ly/3hs2Vxr"

    # Open Image from respose content
    response = requests.get(image_url)
    image = Image.open(BytesIO(response.content))

    # Get terminal height
    height, _ = terminal_size()

    # Choose an art style
    style = "gradient"  # or "braille"

    # Define drawer
    drawer = new_drawer(style, height=height, colorize=True)

    # Iterate over frames
    texts = []
    for frame_id in range(image.n_frames):
        # Select frame
        image.seek(frame_id)

        # Save output for frame
        texts.append(drawer(image))

    # Iterate over saved outputs in a circular manner
    num_frames = len(texts)
    counter = 0
    while True:
        # Refresh
        clear_screen()

        # Print output
        print(texts[counter])

        # Set a delay between frames
        time.sleep(1 / num_frames)

        # Circular increment
        counter = (counter + 1) % num_frames

Note

Although this example uses an animated GIF as input, the same principle can be applied to static images from the web.

Warning

This example requires the requests library.