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

发布时间 2023-09-25 15:28:37作者: 槑孒

错误由来

im = im.resize((w, h), Image.ANTIALIAS)

Traceback (most recent call last):
  AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
import PIL
print(PIL.__version__)
10.0.1

原因是在pillow的10.0.0版本中,ANTIALIAS方法被删除了,使用新的方法即可:

Image.LANCZOS
Image.Resampling.LANCZOS

解决办法

方案一,修改方法调用,将其中的ANTIALIAS替换为LANCZOS

# im = im.resize((w, h), Image.ANTIALIAS)
im = im.resize((w, h), Image.LANCZOS)

方案二,降级Pillow的版本,如9.5.0版本

卸载再重新安装

pip uninstall -y Pillow
pip install Pillow==9.5.0