Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JoelOnyedika opened this issue Nov 9, 2024 · 3 comments
Labels
bug Issues that report (apparent) bugs.

Comments

@JoelOnyedika
Copy link

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

  • MoviePy version: 1.0.3
  • Python version: 3.9+
  • Pillow version: 10.0.0+
  • Operating System: Tested on Windows, Linux

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.

@JoelOnyedika JoelOnyedika added the bug Issues that report (apparent) bugs. label Nov 9, 2024
@Tinny-Robot
Copy link

@JoelOnyedika create a PR with the changes

@vedantroy
Copy link

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

@JoelOnyedika
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues that report (apparent) bugs.
Projects
None yet
Development

No branches or pull requests

3 participants