Module PIL has not attribute "Resampling"

Viewed 15698

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.

6 Answers

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.

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.

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)

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.

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

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

Related