Zulko / moviepy

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

resize.py uses deprecated PIL.Image.ANTIALIAS - needs update for Pillow compatibility #2238

Open JoelOnyedika opened 2 weeks ago

JoelOnyedika commented 2 weeks ago

Description

The current implementation of resize.py uses the deprecated PIL.Image.ANTIALIAS which was removed in newer versions of Pillow. This causes issues for users trying to use MoviePy with current Pillow versions.

Current Behavior

When trying to use the resize functionality with current Pillow versions (>=10.0.0), the following error occurs:

AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'

Expected Behavior

The resize functionality should work with current Pillow versions using the new Image.Resampling.LANCZOS instead of the deprecated ANTIALIAS.

Proposed Solution

Here's a proposed update to the resize implementation that maintains compatibility:

from scipy import ndimage
import numpy as np
from PIL import Image

def resize(clip, newsize=None, height=None, width=None, apply_to_mask=True):
    w, h = clip.size
    if newsize is not None:
        w2, h2 = newsize
    else:
        if width is not None:
            w2 = width
            h2 = int(h * width / w)
        elif height is not None:
            h2 = height
            w2 = int(w * height / h)
        else:
            raise ValueError("Either newsize, width, or height must be specified!")

    # Method 1: Using scipy.ndimage (recommended for better performance)
    resized_clip = clip.fl_image(
        lambda pic: ndimage.zoom(pic, [h2 / h, w2 / w, 1], order=1)
    )

    # Alternative Method 2: Using PIL with updated resampling
    """
    def resize_frame(pic):
        pil_image = Image.fromarray(pic)
        resized_img = pil_image.resize((w2, h2), Image.Resampling.LANCZOS)
        return np.array(resized_img)

    resized_clip = clip.fl_image(resize_frame)
    """

    resized_clip.fps = clip.fps
    resized_clip.duration = clip.duration
    resized_clip.end = clip.end

    return resized_clip

Additional Context

There are two proposed methods in the solution:

  1. Using scipy.ndimage.zoom: Generally better performance for video processing
  2. Using updated PIL resize: More compatible with image processing workflows

Temporary Workaround

For users encountering this issue, there are two temporary solutions:

  1. Pin Pillow to version 9.5.0 (pip install Pillow==9.5.0)
  2. Clone the repository and modify the resize.py file locally

Environment

Impact

This affects all users trying to use MoviePy with current versions of Pillow, particularly in production environments where using older versions of dependencies isn't ideal.

Would appreciate feedback on the proposed solution or alternative approaches.

Tinny-Robot commented 1 week ago

@JoelOnyedika create a PR with the changes

vedantroy commented 1 week ago

Is there a workaround in the meantime that I can I use? I guess I can downgrade pillow.

JoelOnyedika commented 6 days ago

Is there a workaround in the meantime that I can I use? I guess I can downgrade pillow.

Well downgrade your pillow version and it fixes the issue. Honestly for me i just gave up on moviepy, the workarounds are juat too much for my production grade application so ai went to react remotion because its just css bro