AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
Asked Answered
F

9

101
  • I am trying to have images in my Tkinter GUI, hence I am using PIL.
  • Image.ANTIALAIS is not working. However, Image.BILINEAR works

Here's some sample code:

import tkinter as tk
from PIL import Image, ImageTk

window = tk.Tk()

image = Image.open(r"VC.png")
image = image.resize((20, 20), Image.ANTIALIAS)

tk_image = ImageTk.PhotoImage(image)

image_label = tk.Label(window, image=tk_image)
image_label.pack()

window.mainloop()

Here's the error:

Traceback (most recent call last):
  File "<module1>", line 19, in <module>
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
  • I tried reinstalling pip and Pillow. It didn't work.
  • I asked ChatGPT about this, and it advised me to upgrade to Pillow's latest version. I am on the latest version (10.0.0).
Fantom answered 4/7, 2023 at 21:58 Comment(5)
the question needs sufficient code for a minimal reproducible exampleStinkweed
how to ask a good question: stackoverflow.com/help/how-to-askStinkweed
Why do you think that there should be an attribute ANTIALIAS?Dendrology
Backwards compatibility is so 1960 style when IBM had to learn the lesson badly :-)Awhirl
Only @Wolfgang Fahl (in this group) undestood what the problem is about! Yet, the question is so simple and clear and its description contains more than enough information for the problem not only to be grasped but also to be reproduced. Anyway, if you can't grasp the question or the problem, why do you get involved with it at all??Glabrescent
S
18

The problem is with Pillow 10.0.

Trying to uninstall Pillow might give some errors.

Just put this in cmd:

pip install Pillow==9.5.0

Siqueiros answered 22/7, 2023 at 15:2 Comment(1)
Or just use Image.LANCZOSStatolith
H
179

ANTIALIAS was removed in Pillow 10.0.0 (after being deprecated through many previous versions). Now you need to use PIL.Image.LANCZOS or PIL.Image.Resampling.LANCZOS.

(This is the exact same algorithm that ANTIALIAS referred to, you just can no longer access it through the name ANTIALIAS.)

Reference: Pillow 10.0.0 release notes (with table of removed constants)


Simple code example:

import PIL
import numpy as np

# Gradient image with a sharp color boundary across the diagonal
large_arr = np.fromfunction(lambda x, y, z: (x+y)//(z+1),
                            (256, 256, 3)).astype(np.uint8)
large_img = PIL.Image.fromarray(large_arr)

# Resize it: PIL.Image.LANCZOS also works here
small_img = large_img.resize((128, 128), PIL.Image.Resampling.LANCZOS)
print(small_img.size)

large_img.show()
small_img.show()

Headley answered 4/7, 2023 at 22:25 Comment(9)
pip install Pillow==9.5.0 workaround for old code.Antinucleon
an example of code would help, I still have the error with Image.Resampling.LANCZOSJoaniejoann
See edit. If you still have issues, you could post a question stating the PIL version and the error message you get.Headley
I like how the abbreviation LANCZOS forces users to visit stackoverflow - very good marketing for the platform by the Pillow folks! ChatGPT will not guess this with it's 2021 cut-off time. We need to do more things to confuse users and ChatGPT!Awhirl
Ig the Pillow folks are in partnership with Stackoverflow at this point. Cuz I am seeing a lot of errors coming up. XDFantom
I have added this in the first py script, to work around the issue. maybe it helps someone: import PIL PIL.Image.ANTIALIAS = PIL.Image.LANCZOSRapallo
@Antinucleon while your answer may be helpful also note that every version less than 10.x is vulnerable to CVE-2023-4863 published on libwebpFrontwards
Congratulations and thank you! This saved a project of mine!Griswold
If you use conda for env and package management, Pillow 9.5 isn't available in the conda standard channel. Use conda install pillow=9.4 instead. It worked for me.Sit
S
18

The problem is with Pillow 10.0.

Trying to uninstall Pillow might give some errors.

Just put this in cmd:

pip install Pillow==9.5.0

Siqueiros answered 22/7, 2023 at 15:2 Comment(1)
Or just use Image.LANCZOSStatolith
N
16

In EasyOCR, the following error appeared:

img = cv2.resize(img, (int(model_height*ratio), model_height), interpolation=Image.ANTIALIAS)AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'

I did the following:

pip uninstall Pillow
pip install Pillow==9.5.0

The problem must be with Pillow version 10.0.

Norty answered 20/7, 2023 at 6:12 Comment(1)
After downgrading the Pillow version to 9.5.0, I got the following error from numpy package. (I did not use this package anywhere in my code)Tillford
K
7

You can add the below before your code as a quick patch.

from PIL import Image as pil
from pkg_resources import parse_version

if parse_version(pil.__version__)>=parse_version('10.0.0'):
    Image.ANTIALIAS=Image.LANCZOS
Katlynkatmai answered 5/10, 2023 at 11:8 Comment(0)
L
5

Downgrading using pip worked for me:

pip3 install "Pillow<10.0.0"
Lucas answered 18/12, 2023 at 21:7 Comment(0)
S
4

Hacky way but worked:

In the utils.py file of easyOCR/Scripts, I replaced the ANTIALIAS with LANCZOS.

Staceestacey answered 31/7, 2023 at 19:57 Comment(2)
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.Navel
This will work only for your specific machine. It would throw an error when distributing the code. So when your manager asks why it didn't work on the client's machine, just say "It works on my machine " XDFantom
V
4

ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use LANCZOS or Resampling.LANCZOS instead.

Ventage answered 7/11, 2023 at 20:28 Comment(0)
O
3

I simply replaced my line img.thumbnail(output_size, Image.ANTIALIAS) with img.thumbnail(output_size, Image.Resampling.LANCZOS) and it worked.

This issue started from Pillow 7.0 release.

Ocd answered 9/2 at 18:18 Comment(0)
S
0

Also if you are using easyocr make sure to upgrade at least to version 1.7.1

Signor answered 19/8 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.