Zulko / moviepy

Video editing with Python
https://zulko.github.io/moviepy/
MIT License
12.1k stars 1.51k forks source link

Drop Shadow #2026

Open AyeshaIrshad1337 opened 11 months ago

AyeshaIrshad1337 commented 11 months ago

Here is my code but its not adding drop shadow ... i dont know whats wrong with it


from PIL import Image, ImageDraw, ImageFont, ImageFilter

class ShadowTextClip(TextClip):
    def __init__(self, txt, font=None, shadowcolor="black", shadowoffset=(3,3), shadowblur=5, **kwargs):
        super().__init__(txt, font=font, **kwargs)
        self.shadowcolor = shadowcolor
        self.shadowoffset = shadowoffset
        self.shadowblur = shadowblur

    def make_frame(self, t):
        im = super().make_frame(t)
        if im is not None:
            # Convert the frame to a PIL image
            frame = Image.fromarray(im).convert('RGBA')

            # Create a new image with an alpha layer (RGBA)
            base = Image.new('RGBA', frame.size, (0,0,0,0))

            # Create a draw object and draw the text
            draw = ImageDraw.Draw(base)
            draw.text(self.shadowoffset, self.txt, font=self.font, fill=self.shadowcolor)

            # Apply the blur filter to the shadow layer
            shadow = base.filter(ImageFilter.BLUR)
            for i in range(self.shadowblur):
                shadow = shadow.filter(ImageFilter.BLUR)

            # Paste the shadow layer on top of the original image
            frame.paste(shadow, (0, 0), shadow)

            # Paste the text on top of the shadow layer
            draw = ImageDraw.Draw(frame)
            draw.text((0, 0), self.txt, font=self.font, fill=self.color)

            # Convert the resulting image back to a numpy array
            return np.array(frame)
        return im

# Load the video clip
clip=VideoFileClip("/content/drive/MyDrive/2 Youtube Long 12 Sec Loop BG.mp4")
# Set the text and font
text = "Hello, World!"
font = 'Arial'

# Create a ShadowTextClip for the text with a drop shadow
# Create a ShadowTextClip for the text with a drop shadow
text_clip = ShadowTextClip(text, font=font, color='white', fontsize=70, shadowcolor='gray', shadowblur=10)

# Set the position of the text
text_clip = text_clip.set_pos("center")

# Overlay the text on the video clip
final_clip = CompositeVideoClip([clip, text_clip.set_duration(3)])
# Set the duration of the final clip to the duration of the original clip
final_clip.duration = clip.duration

# Write the resulting video to a file
final_clip.write_videofile('video.mp4')```