If you're encountering the error AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS', don't worry - this is a common issue that's easy to fix. Here's what's happening and how to resolve it.
This error typically appears in one of two scenarios:
Pillow version 10.0.0 or higher: The ANTIALIAS constant was deprecated and then removed in favor of Resampling.LANCZOS.
Incorrect import: You might be importing PIL.Image incorrectly.
from PIL import Image
# Replace this:
# image = image.resize((width, height), Image.ANTIALIAS)
# With this:
image = image.resize((width, height), Image.Resampling.LANCZOS)
If you need backward compatibility:
try:
from PIL import Image, ImageFilter
resampling = Image.Resampling.LANCZOS
except ImportError:
resampling = Image.ANTIALIAS
image = image.resize((width, height), resampling)
If you must use the old constant (not recommended):
Image.ANTIALIAS = Image.Resampling.LANCZOS # Workaround for old code
Always use the current Resampling.LANCZOS method as it's the officially supported approach in modern Pillow versions. This ensures your code will keep working with future updates.
Remember to update your Pillow package regularly:
pip install --upgrade pillow