AttributeError: module ‘PIL.Image’ has no attribute ‘register_extensions


(dinesh) #1

hi, i just started going through lesson-1 of fast.ai part1-v2, in google-colab. Asi was going through line by line along with the lecture video i was stuck when i came to these lines of code

img = plt.imread(f’{PATH}valid/cats/{files[0]}’)
plt.imshow(img);

when i ran these lines i got the following error

AttributeError: module ‘PIL.Image’ has no attribute ‘register_extensions

i googled the problem and the closest solution i got was to “import PIL.Image” which i did and i still got the same error.

i’m using windows 10.

without this step i cant go any further . so i request you guys to help me
thank you


(Asif Imran) #2

I had similar issue. Next time, you start your colab VM, be sure to comment out the following two lines (i.e do not run them)

#%reload_ext autoreload        <------------— comment out 
#%autoreload 0                 <------------— comment out
%matplotlib inline

Just to be safe, I also reinstalled my PIL

!pip install --no-cache-dir -I pillow

Hopefully, that will take care of the issue.


I am getting AttributeError: module 'PIL' has no attribute 'Image'
(Shubham Malik) #3

I also spent the few hours searching, for the cause. I think now that I have found the issue. There is actually no function with the name register_extensions() but register_extension(). I have created the PR on the official GitHub Repository. Let’s hope for the best.
I think it is the cause of the error.:sunglasses:


(Jeremy Howard) #4

Which module was causing this error? Could you please provide a link to your PR @shubham827 ?


(Shubham Malik) #5


The error I am getting is AttributeError: module 'PIL.Image' has no attribute 'register_extensions'
There are others also who are facing the same issue

Well it turns out that the code for which I created the PR is indeed correct. Please see the PR for further info.


(Jeremy Howard) #6

Is this only happening on Google Colab?


#7

I have used Google Colab only and facing the same error.


(Karim Elawaad) #8

Adding the following lines right before the imports cell fixed the problem for me

!pip install Pillow==4.0.0
!pip install PIL
!pip install image
import PIL.image


(Shubham Malik) #9

@kelawaad I have used the above lines with PIL.Image even then I am getting the same error.
I saw these lines while importing, torchvision is installing the Pillow 5.0.0 so whats the use of first line.

Installing collected packages: pillow, torchvision
  Found existing installation: Pillow 4.0.0
    Uninstalling Pillow-4.0.0:
      Successfully uninstalled Pillow-4.0.0
Successfully installed pillow-5.0.0 torchvision-0.2.0

Can you confirm your pillow version using
!pip show pillow

@jeremy I have tried it on Kaggle Kernels too but it crashes. Google CoLab keeps giving this error.


(Karim Elawaad) #10

@shubham827 Pillow version is 4.0.0 and is verified by running

!pip show pillow

Output:

Name: Pillow
Version: 4.0.0
Summary: Python Imaging Library (Fork)

Did you run !pip install Pillow=4.0.0 after running !pip install torchvision?


(Shubham Malik) #11

No, I used them as you said in your comment before the imports.
But now I have used it after torchvision and now it is solved.
Thank you


(Matthew Rosenthal) #12

I typically use scipy.misc.imread instead of plt.imread.

And early on i do a
import scipy.misc


(Fabian Linzberger) #13

I also ran into this issue while using colab.
I think the problem is that Pillow==4.0.0 is preinstalled on colab and when you upgrade the package or install packages that require a newer version (5.0.0 in my case) there can be a mismatch between the api of the two versions.
For me restarting the runtime using “Runtime / Restart runtime…” from the menu after installing the newer Pillow using pip and running the code that uses it fixed the issue.


(Ricardo Herrmann) #14

Another workaround:

def register_extension(id, extension):
    PIL.Image.EXTENSION[extension.lower()] = id.upper()
PIL.Image.register_extension = register_extension
def register_extensions(id, extensions):
    for extension in extensions:
        register_extension(id, extension)
PIL.Image.register_extensions = register_extensions

(Ryan) #15

Hi all,

I’ve been trying different workarounds and even used some of the member’s helpful comments here, but I am still getting this error. I’ve also managed to “break” some other things, but I’m sure I can fix those with a fresh instance. Any and all help debugging is greatly appreciated.

Thanks everyone


(Eternal) #16

Thanks! This fixed it for me too!


(Sharath) #17

@lefant thanks for the solution… I can confirm restarting the runtime resolves this issue.


(Ben Hutchison) #18

This workaround seems to fix it for me


(XW) #19

This one worked for me with a minor change as following:

def register_extension(id, extension):
    Image.EXTENSION[extension.lower()] = id.upper()
Image.register_extension = register_extension
def register_extensions(id, extensions):
    for extension in extensions:
        register_extension(id, extension)
Image.register_extensions = register_extensions

(Bharat Kunwar) #20

I found that simply restarting runtime after installing the required packages at the top solved the issue for me.