Add Fade Effects For Text With Python

In this article, I will use the Python Imaging Library (PIL) to create fade-in and fade-out text animations.

My approach involves creating frames where each subsequent frame has a higher opacity for the text until it is fully opaque. We will then save these frames as a GIF, as PIL alone doesn’t handle video creation directly.

Import PIL functions

Let’s import some functions from the PIL library. These functions will be used to create images, insert text, and alter the transparency of the frames.

from PIL import Image, ImageDraw, ImageFont

Set parameters for fading animations

Next we can set some input parameters for the fading animations.

# parameters
text = "Hello, World!"
width, height = 300, 50
font_size = 21
num_frames = 60  # Number of frames for the fading effect

Fade-in animation for text

I will first create a base image with a solid color. This base image will have four channels – Red, Green, Blue, and Alpha (for transparency).

bg_image = Image.new("RGBA", (width, height), color=(0, 50, 0, 255))

Next, to put text over the base image, I will use the Arial font and for that, I will load the arial.tff file.

font = ImageFont.truetype("arial.ttf", font_size)

You can also use PIL’s default font by not specifying or find a suitable .ttf file.

Now I will find the coordinates to put the text string at the center of the image.

draw = ImageDraw.Draw(bg_image)
text_width, text_height = draw.textsize(text, font=font)
text_x = (width - text_width) / 2
text_y = (height - text_height) / 2

After this, I will create multiple frames in which the text would appear on the base image. The transparency will reduce gradually from the first frame to the last.

# empty list to store processed frames
images = []

# Generate frames
for frame in range(num_frames):
    # Create an image for the text with partial opacity
    txt_image = Image.new("RGBA", (width, height), (255, 255, 255, 0))
    draw = ImageDraw.Draw(txt_image)
    # increasing opacity
    opacity = int(255 * (frame + 1) / num_frames)  
    # add text
    draw.text((text_x, text_y), text, font=font, fill=(255, 255, 255, opacity))

    # blend the base image with the text image
    blended_image = Image.alpha_composite(bg_image, txt_image)
    
    # convert the frame from RGBA mode to RGB before adding it to the list
    images.append(blended_image.convert("RGB"))

Now these frames can be combined to form a video or a GIF animation.

# create a GIF
gif_path = "fade_in_effect.gif"
images[0].save(gif_path, save_all=True, append_images=images[1:], duration=240, loop=0)

You can change the value of the duration parameter to speed up or slow down the fade-in animation.

fade-in effect using python

Fade-out animation for text

The same approach can be used to easily implement fade-out text effects as well. Instead of increasing the opacity of the text, here I will decrease the opacity of the text so that it will appear like the text is fading out.

images = []

for frame in range(num_frames):
    # Create an image for the text with decreasing opacity
    txt_image = Image.new("RGBA", (width, height), (255, 255, 255, 0))
    draw = ImageDraw.Draw(txt_image)
    opacity = int(255 * (num_frames - frame) / num_frames)  # Decreasing opacity
    draw.text((text_x, text_y), text, font=font, fill=(255, 255, 255, opacity))

    # Blend the base image with the text image
    blended_image = Image.alpha_composite(bg_image, txt_image)

    images.append(blended_image.convert("RGB"))

# create a GIF
gif_path = "fade_out_effect.gif"
images[0].save(gif_path, save_all=True, append_images=images[1:], duration=120, loop=0)
fade-out effect using python

Conclusion

In this tutorial, fade-in and fade-out animations were created using Python. PIL library was used to create multiple frames of text over a blank background image with varying text transparency. Using Python you can create other attractive effects for text as well such as scrolling text, typewriter effect, or glowing text.

Leave a Reply

Your email address will not be published. Required fields are marked *