ash1
ash1

Reputation: 453

Module PIL has not attribute "Resampling"

I have run the same code(with packages I needed) before and it worked, not sure what's happening now. This show the error, AttributeError: module 'PIL.Image' has no attribute 'Resampling'. Probably it's small issue, but I can't figure it out, I am working in databricks.

Upvotes: 26

Views: 65741

Answers (13)

sokobanz
sokobanz

Reputation: 1

Well, I had the same issue, so I went with simplest solution by TADA!@! removing it. also it can be fixed with changing it to Image.BICUBIC. results are just the same for me. working code with same valid results.

Upvotes: 0

Raj Rao
Raj Rao

Reputation: 9138

I had the same issue occur in Google Colab.

I first installed Pillow:

!pip install pillow

After that I still got the error, until I restarted the Runtime. Once the runtime was restarted, the error went away.

Upvotes: 0

Nikita Agrawal
Nikita Agrawal

Reputation: 61

install matplotlib==3.7.1

This worked for me!

Upvotes: 3

Vladimir Panteleev
Vladimir Panteleev

Reputation: 25187

If you've just installed a Python program with pip and it raises this error, it's probably because of a conflict with something you've previously installed. In this case pip install Pillow==9.0.0 did not work for me (and it may break something else).

Creating a clean venv for that program should fix it:

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

Upvotes: 1

Febrin
Febrin

Reputation: 109

If you don't want to downgrade the Pillow library, you can try to install the latest matplotlib:

python3 -m pip install matplotlib==3.7.1

Upvotes: 2

Mike Smith
Mike Smith

Reputation: 11

Resampling enum (and other new enums) can be added here. https://github.com/python/typeshed/blob/main/stubs/Pillow/PIL/Image.pyi

Upvotes: 1

hat black
hat black

Reputation: 81

my way to fix it: pip install --ignore-installed Pillow==9.3.0

Upvotes: 8

Khawar Islam
Khawar Islam

Reputation: 2914

In my case, pip install Pillow==9.1.0 is suitable for my code

Upvotes: 2

Hugues
Hugues

Reputation: 3170

I find that forcing the use of a particular Pillow version may break other packages.

Another solution that works for any Pillow version and avoids deprecation warnings is to insert the code:

  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.

Upvotes: 13

aposch72
aposch72

Reputation: 301

I had the same problem and found that I need to replace PIL.Image.Resampling.BICUBIC with PIL.Image.BICUBIC. I am using pillow version 7.1.2

from PIL import Image

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

Upvotes: 20

SuddenWind
SuddenWind

Reputation: 61

Same happened when I upgraded some module. I just restarted runtime and it helped.

Upvotes: 4

YuwinZHANG
YuwinZHANG

Reputation: 299

I had the same problem. The easy way is use the old version of Pillow.

pip install Pillow==9.0.0

And your code should work.

Note: You can also use

pip install --ignore-installed Pillow==9.0.0

If for some reason, pip is refusing to install it. Note, however, that it can break dependencies, so use it only as a last resort.

Upvotes: 29

AKX
AKX

Reputation: 169051

The resampling enums were seemingly added in Pillow 9.1.0 (released 3 days ago) via this pull request.

I would imagine your Databricks environment has a different verison.

Upvotes: 12

Related Questions