PIL image.resize() 报错AttributeError: module ‘PIL.Image‘ has no attribute ‘ANTIALIAS‘ 解决方案
使用PIL读取图像后对其进行Resize时由于PIL 版本问题出现`AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'`,简要介绍相关的Debug过程
文章共792字 · 阅读需要大约3分钟
一键AI生成摘要,助你高效阅读
问答
·
问题描述
使用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.LANCZOS
或 Image.Resampling.LANCZOS
修改后的代码如下:
image = image.resize((scaled_width, scaled_height), Image.LANCZOS)
以上仅记录以下自己在Debug过程中遇到的问题,方便后续修改类似的程序。
参考:
更多推荐
已为社区贡献3条内容
所有评论(0)