PIL ImageDraw.Draw attribute error 'Image' has no attribute 'load'

Viewed 91

I am trying to draw the text 'we made it' onto an imported gif and display it on my notebook:

my_image = Image(url = 'https://miro.medium.com/max/1000/1*AmI9wRbXrfIWGESx6eEiTw.gif')

image_editable = ImageDraw.Draw(my_image)
image_editable.text((15,15), "We made it", (237, 230, 211), font=title_font)
my_image.show()

But I am getting the error:

AttributeError                            Traceback (most recent call last)
~\anaconda3\lib\site-packages\PIL\ImageDraw.py in Draw(im, mode)
    767     try:
--> 768         return im.getdraw(mode)
    769     except AttributeError:

AttributeError: 'Image' object has no attribute 'getdraw'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17400/3060345407.py in <module>
      1 my_image = Image(url = 'https://miro.medium.com/max/1000/1*AmI9wRbXrfIWGESx6eEiTw.gif')
      2
----> 3 image_editable = ImageDraw.Draw(my_image)
      4 image_editable.text((15,15), "We made it", (237, 230, 211), font=title_font)
      5 my_image.show()

~\anaconda3\lib\site-packages\PIL\ImageDraw.py in Draw(im, mode)
    768         return im.getdraw(mode)
    769     except AttributeError:
--> 770         return ImageDraw(im, mode)
    771
    772

~\anaconda3\lib\site-packages\PIL\ImageDraw.py in __init__(self, im, mode)
     56            defaults to the mode of the image.
     57         """
---> 58         im.load()
     59         if im.readonly:
     60             im._copy()  # make it writeable

AttributeError: 'Image' object has no attribute 'load'

How can I get rid of these errors?

1 Answers

The reason why you're getting an error is because you aren't supposed to be passing the url to the Image class and then pass this instantiated class to the ImageDraw.Draw(my_image).

What you're supposed to do is use Image.open() then pass that resulting value to ImageDraw.Draw().

According to the ImageDraw module docs, you need to have the file available locally so you need to actually grab that file and then do the following:

# assume you have the file downloaded as file.gif

my_image = Image.open('file.gif')

image_editable = ImageDraw.Draw(my_image)
image_editable.text((15,15), "We made it", (237, 230, 211), font=title_font)
my_image.show()
Related