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

问题描述 投票:0回答:13

我之前运行过相同的代码(带有我需要的包)并且它有效,不确定现在发生了什么。这显示错误,

AttributeError: module 'PIL.Image' has no attribute 'Resampling'
。可能这是一个小问题,但我无法弄清楚,我正在 databricks 工作。

python python-imaging-library resampling
13个回答
28
投票

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

pip install Pillow==9.0.0

你的代码应该可以工作。

注意:您也可以使用

pip install --ignore-installed Pillow==9.0.0

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


16
投票

我遇到了同样的问题,发现我需要将

PIL.Image.Resampling.BICUBIC
替换为
PIL.Image.BICUBIC
。 我使用的是枕头版本7.1.2

from PIL import Image

im = Image.open('image.png')
im2 = im.resize((512,512),resample=Image.BICUBIC)
display(im2)

9
投票

我发现强制使用特定的 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.

7
投票

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

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


6
投票

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


3
投票

当我升级某些模块时也发生了同样的情况。我刚刚重新启动运行时,它有帮助。


2
投票

如果您不想降级

Pillow
库,您可以尝试安装最新的
matplotlib

python3 -m pip install matplotlib==3.7.1


2
投票

安装matplotlib==3.7.1

这对我有用!


1
投票

就我而言,

pip install Pillow==9.1.0
适合我的代码


1
投票

可以在此处添加重新采样枚举(和其他新枚举)。 https://github.com/python/typeshed/blob/main/stubs/Pillow/PIL/Image.pyi


1
投票

如果您刚刚使用

pip
安装了 Python 程序并引发了此错误,则可能是因为与您之前安装的程序发生冲突。在这种情况下,
pip install Pillow==9.0.0
对我不起作用(并且可能会破坏其他东西)。

为该程序创建一个干净的 venv 应该可以修复它:

$ mkdir frobnicator
$ cd frobnicator
$ python -m venv venv
$ source venv/bin/activate
(venv) $ pip install frobnicator
(venv) $ frobnicator ...

0
投票

我在 Google Colab 中也出现了同样的问题。

我第一次安装Pillow:

!pip install pillow

之后我仍然收到错误,直到我重新启动运行时。重新启动运行时后,错误就消失了。


0
投票

嗯,我遇到了同样的问题,所以我采用了最简单的解决方案 多田!@!删除它。也可以通过将其更改为 Image.BICUBIC 来修复它。结果对我来说是一样的。具有相同有效结果的工作代码。

© www.soinside.com 2019 - 2024. All rights reserved.