PIL image.resize() 报错AttributeError: module ‘PIL.Image‘ has no attribute ‘ANTIALIAS‘ 解决方案

问题描述

使用PIL读取图像后对其进行Resize时由于PIL 版本问题出现AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
具体的代码如下

image = image.resize((scaled_width, scaled_height), Image.ANTIALIAS)

原因分析

在新版本pillow(10.0.0之后)Image.ANTIALIAS 被移除了,取而代之的是Image.LANCZOS or Image.Resampling.LANCZOS,相关描述可以可以在pillow的releasenotes中查到。
在这里插入图片描述
更多被移除的方法如下

Removed Use instead
Image.LINEAR Image.BILINEAR or Image.Resampling.BILINEAR
Image.CUBIC Image.BICUBIC or Image.Resampling.BICUBIC
Image.ANTIALIAS Image.LANCZOS or Image.Resampling.LANCZOS
ImageCms.INTENT_PERCEPTUAL ImageCms.Intent.PERCEPTUAL
ImageCms.INTENT_RELATIVE_COLORMETRIC ImageCms.Intent.RELATIVE_COLORMETRIC
ImageCms.INTENT_SATURATION ImageCms.Intent.SATURATION
ImageCms.INTENT_ABSOLUTE_COLORIMETRIC ImageCms.Intent.ABSOLUTE_COLORIMETRIC
ImageCms.DIRECTION_INPUT ImageCms.Direction.INPUT
ImageCms.DIRECTION_OUTPUT ImageCms.Direction.OUTPUT
ImageCms.DIRECTION_PROOF ImageCms.Direction.PROOF
ImageFont.LAYOUT_BASIC ImageFont.Layout.BASIC
ImageFont.LAYOUT_RAQM ImageFont.Layout.RAQM
BlpImagePlugin.BLP_FORMAT_JPEG BlpImagePlugin.Format.JPEG
BlpImagePlugin.BLP_ENCODING_UNCOMPRESSED BlpImagePlugin.Encoding.UNCOMPRESSED
BlpImagePlugin.BLP_ENCODING_DXT BlpImagePlugin.Encoding.DXT
BlpImagePlugin.BLP_ENCODING_UNCOMPRESSED_RAW_RGBA BlpImagePlugin.Encoding.UNCOMPRESSED_RAW_RGBA
BlpImagePlugin.BLP_ALPHA_ENCODING_DXT1 BlpImagePlugin.AlphaEncoding.DXT1
BlpImagePlugin.BLP_ALPHA_ENCODING_DXT3 BlpImagePlugin.AlphaEncoding.DXT3
BlpImagePlugin.BLP_ALPHA_ENCODING_DXT5 BlpImagePlugin.AlphaEncoding.DXT5
FtexImagePlugin.FORMAT_DXT1 FtexImagePlugin.Format.DXT1
FtexImagePlugin.FORMAT_UNCOMPRESSED FtexImagePlugin.Format.UNCOMPRESSED
PngImagePlugin.APNG_DISPOSE_OP_NONE PngImagePlugin.Disposal.OP_NONE
PngImagePlugin.APNG_DISPOSE_OP_BACKGROUND PngImagePlugin.Disposal.OP_BACKGROUND
PngImagePlugin.APNG_DISPOSE_OP_PREVIOUS PngImagePlugin.Disposal.OP_PREVIOUS
PngImagePlugin.APNG_BLEND_OP_SOURCE PngImagePlugin.Blend.OP_SOURCE
PngImagePlugin.APNG_BLEND_OP_OVER PngImagePlugin.Blend.OP_OVER

回到刚刚的问题

解决方案

对于刚刚的问题 解决方案就是将Image.ANTIALIAS 替换为 Image.LANCZOSImage.Resampling.LANCZOS
修改后的代码如下:

image = image.resize((scaled_width, scaled_height), Image.LANCZOS)

以上仅记录以下自己在Debug过程中遇到的问题,方便后续修改类似的程序。

参考:

  1. AttributeError: module ‘PIL.Image‘ has no attribute ‘ANTIALIAS‘
  2. Pillow 10.0.0 documentation

猜你喜欢

转载自blog.csdn.net/fovever_/article/details/134690657
今日推荐