AttributeError:模块“PIL.Image”没有属性“ANTIALIAS”

MT_*_*276 63 python python-imaging-library

  • 我试图在我的 Tkinter GUI 中包含图像,因此我使用PIL
  • Image.ANTIALAIS不起作用。然而,Image.BILINEAR有效

这是一些示例代码:

import tkinter as tk
from PIL import Image, ImageTk

window = tk.Tk()

image = Image.open(r"VC.png")
image = image.resize((20, 20), Image.ANTIALIAS)

tk_image = ImageTk.PhotoImage(image)

image_label = tk.Label(window, image=tk_image)
image_label.pack()

window.mainloop()
Run Code Online (Sandbox Code Playgroud)

这是错误:

Traceback (most recent call last):
  File "<module1>", line 19, in <module>
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
Run Code Online (Sandbox Code Playgroud)
  • 我尝试重新安装 pip Pillow。它不起作用。
  • 我向ChatGPT询问了此事,它建议我升级到 Pillow 的最新版本。我使用的是最新版本(10.0.0)。

slo*_*rop 118

ANTIALIAS在 Pillow 10.0.0 中被删除(在许多以前的版本中被弃用后)。现在您需要使用PIL.Image.LANCZOSPIL.Image.Resampling.LANCZOS

(这与所引用的算法完全相同ANTIALIAS,只是您无法再通过名称访问它ANTIALIAS。)

参考:Pillow 10.0.0 发行说明(包含已删除常量表)


简单代码示例:

import PIL
import numpy as np

# Gradient image with a sharp color boundary across the diagonal
large_arr = np.fromfunction(lambda x, y, z: (x+y)//(z+1),
                            (256, 256, 3)).astype(np.uint8)
large_img = PIL.Image.fromarray(large_arr)

# Resize it: PIL.Image.LANCZOS also works here
small_img = large_img.resize((128, 128), PIL.Image.Resampling.LANCZOS)
print(small_img.size)

large_img.show()
small_img.show()

Run Code Online (Sandbox Code Playgroud)

  • 旧代码的“pip install Pillow==9.5.0”解决方法。 (30认同)
  • 代码示例会有所帮助,我仍然遇到 Image.Resampling.LANCZOS 错误 (2认同)

小智 12

EasyOCR中,出现以下错误:

img = cv2.resize(img, (int(model_height*ratio), model_height), interpolation=Image.ANTIALIAS)AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
Run Code Online (Sandbox Code Playgroud)

我做了以下事情:

img = cv2.resize(img, (int(model_height*ratio), model_height), interpolation=Image.ANTIALIAS)AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
Run Code Online (Sandbox Code Playgroud)

问题一定出在 Pillow 版本 10.0 上。


小智 12

问题出在Pillow 10.0 上。

尝试卸载 Pillow 可能会出现一些错误。

只需将其放入cmd:

pip install Pillow==9.5.0

  • 或者只使用“Image.LANCZOS” (4认同)