Module PIL has not attribute "Resampling"
Asked Answered
R

13

22

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.

Raimes answered 4/4, 2022 at 13:36 Comment(1)
Please add your code and the full error so we can help you.Rodas
T
29

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.

Tellus answered 9/4, 2022 at 22:47 Comment(3)
Doing this gave yet another error: AttributeError: module 'PIL.TiffTags' has no attribute 'IFD'Copolymerize
works on colab.Boomerang
I would not recommend using outdated libraries. Not only will you miss out on improvements, but there are security concerns, and it makes it more likely that you'll run into compatibility issues with other libraries. It's much better to just update your code, as aposch72 explains in their answer: https://mcmap.net/q/567806/-module-pil-has-not-attribute-quot-resampling-quotInviolate
E
19

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)
Erogenous answered 23/5, 2022 at 10:15 Comment(0)
A
12

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.
Aurelioaurelius answered 16/7, 2022 at 1:38 Comment(1)
This seems it might help people developing libraries they want to keep compatible with old code. But for most applications, it's better for developers to be aware of what versions of libraries they're using, and just write code that works with those versions. It's good to get into the habit of creating virtual environments for development, install the latest versions of libraries, and use pip freeze to find out what those versions are, so they can be used for production. This is a pretty good place to start: packaging.python.org/en/latest/tutorials/installing-packagesInviolate
S
9

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.

Snapdragon answered 4/4, 2022 at 13:41 Comment(0)
E
7

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

Eclogue answered 8/12, 2022 at 13:45 Comment(2)
That is a copy (with different version since it was 8 months ago) of a previous answer. Please, edit to clarify how this answer is different, and better, than previous answer.Haerle
This version is what worked for me with fastai, without the --ignore-installed though.Pyrrolidine
E
4

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

Eddi answered 20/4, 2022 at 9:38 Comment(0)
N
3

install matplotlib==3.7.1

This worked for me!

Ninon answered 7/4, 2023 at 20:6 Comment(0)
E
2

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

Emarie answered 23/9, 2022 at 7:13 Comment(0)
C
2

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

Crest answered 6/3, 2023 at 15:5 Comment(0)
H
1

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

Highspirited answered 1/3, 2023 at 6:41 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewDurmast
S
1

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 ...
Spirited answered 5/4, 2023 at 10:42 Comment(0)
H
0

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.

Hengist answered 2/5, 2023 at 20:0 Comment(0)
B
0

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.

Bunch answered 13/8, 2023 at 5:55 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Anaemia

© 2022 - 2024 — McMap. All rights reserved.