模块 PIL 没有属性“重新采样”

ash*_*sh1 21 python resampling python-imaging-library

我之前运行过相同的代码(带有我需要的包)并且它有效,不确定现在发生了什么。这显示了错误, AttributeError: module 'PIL.Image' has no attribute 'Resampling'。可能这是一个小问题,但我无法弄清楚,我正在数据块中工作。

小智 29

我有同样的问题。最简单的方法是使用旧版本的 Pillow。

pip install Pillow==9.0.0
Run Code Online (Sandbox Code Playgroud)

并且您的代码应该可以工作。

注意:您还可以使用

pip install --ignore-installed Pillow==9.0.0
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因,pip 拒绝安装它。但请注意,它可能会破坏依赖关系,因此仅将其用作最后的手段。

  • 这样做又产生了另一个错误: AttributeError: module 'PIL.TiffTags' has no attribute 'IFD' (2认同)
  • 在 colab 上工作。 (2认同)

小智 18

我遇到了同样的问题,发现我需要替换PIL.Image.Resampling.BICUBICPIL.Image.BICUBIC. 我使用的是枕头版本7.1.2

from PIL import Image

im = Image.open('image.png')
im2 = im.resize((512,512),resample=Image.BICUBIC)
display(im2)
Run Code Online (Sandbox Code Playgroud)


Hug*_*ues 12

我发现强制使用特定的 Pillow 版本可能会破坏其他软件包。

适用于任何 Pillow 版本并避免弃用警告的另一个解决方案是插入代码:

  import PIL.Image
  if not hasattr(PIL.Image, 'Resampling'):  # Pillow<9.0
    PIL.Image.Resampling = PIL.Image
  # Now PIL.Image.Resampling.BICUBIC is always recognized.
Run Code Online (Sandbox Code Playgroud)


AKX*_*AKX 9

重采样枚举似乎是通过此拉取请求添加到Pillow 9.1.0(3 天前发布)中的。

我想您的 Databricks 环境有不同的版本。


小智 7

我的修复方法: pip install --ignore-installed Pillow==9.3.0