[Python] Moviepy : how to add a background color with a stroke line to a Text that overlay a...

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to add a white background to my text with moviepy and I want to add a black stroke line to this background.

    For the moment I have that

    for step in data["steps"]:
    image_file = f"image{str(idx)}.png" # Replace with your actual image file names
    text1 = TextClip(wrap_text(step["text1"]), fontsize=48, color='black', font='Stanberry.ttf')
    text2 = TextClip(wrap_text(step["text2"]), fontsize=48, color='black', font='Stanberry.ttf')


    imageDuration = 5
    videoDuration += imageDuration
    image = ImageClip(image_file).set_duration(imageDuration).resize(height=1920).resize(width=1080).margin(5, color=(0, 0, 0))

    text1_bg = ColorClip(size=(text1.size[0] + 20, text1.size[1] + 20), color=(255, 255, 255)).set_duration(imageDuration)
    text2_bg = ColorClip(size=(text2.size[0] + 20, text2.size[1] + 20), color=(255, 255, 255)).set_duration(imageDuration)

    text1_pos = ('center', 50)
    text2_pos = ('center', image.size[1] - text2.size[1] - 50)

    text1 = text1.set_position(text1_pos).set_duration(imageDuration)
    text2 = text2.set_position(text2_pos).set_duration(imageDuration)

    text1_bg = text1_bg.set_position((image.w // 2 - text1.w // 2 - 10, text1_pos[1] - 10))
    text2_bg = text2_bg.set_position((image.w // 2 - text2.w // 2 - 10, text2_pos[1] - 10))

    composite = CompositeVideoClip([image, text1_bg, text1.set_start(0), text2_bg, text2.set_start(0)], size=(1080, 1920))
    images.append(composite)
    idx += 1

    Continue reading...

Compartilhe esta Página