20

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.

ash1
  • 393
  • 1
  • 2
  • 10

13 Answers13

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.

tornikeo
  • 915
  • 5
  • 20
YuwinZHANG
  • 299
  • 2
  • 2
16

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)
lemon
  • 14,875
  • 6
  • 18
  • 38
aposch72
  • 161
  • 2
10

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.
Hugues
  • 2,865
  • 1
  • 27
  • 39
7

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.

AKX
  • 152,115
  • 15
  • 115
  • 172
6

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

hat black
  • 61
  • 1
  • 1
  • 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. – chrslg Dec 13 '22 at 22:04
  • This version is what worked for me with fastai, without the `--ignore-installed` though. – arun Aug 01 '23 at 20:28
3

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

SuddenWind
  • 41
  • 4
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

Febrin
  • 91
  • 1
  • 2
2

install matplotlib==3.7.1

This worked for me!

1

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

Khawar Islam
  • 2,556
  • 2
  • 34
  • 56
1

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

Mike Smith
  • 11
  • 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 Review](/review/late-answers/33944379) – Niklas Mohrin Mar 07 '23 at 21:33
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 ...
Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
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.

Raj Rao
  • 8,872
  • 12
  • 69
  • 83
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.

sokobanz
  • 1
  • 1
  • 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](/help/how-to-answer). – Community Aug 16 '23 at 06:29