K
K
kramick2021-07-12 22:14:24
Python
kramick, 2021-07-12 22:14:24

Why can't PIL find the open attribute of an Image object?

Hello. I am writing code to add pictures from VKontakte. Ran this code:

def create_icons_users(links_file):
    label = Label(text='')
    with open(links_file, 'r') as r:    # открываем json файл с ссылками
        links = json.load(r)
        for link in links:
            try:
                response = requests.get(link, timeout=10)
            except:
                print('Ошибка!')
            else:
                pil_image = Image.open(BytesIO(response.content)) # тут ошибка
                image = ImageTk.PhotoImage(pil_image)
                label.config(image=image, text='')    # добавляем картинку
                label.image = image
                label.pack()


But an error pops up. What methods of solving the error can you tell me?:
File "path_to_file_code", line 64, in create_icons_users
    <i>pil_image = Image.open(BytesIO(response.content))</i>
<b>AttributeError: type object 'Image' has no attribute 'open'</b>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-07-12
@kramick

The Image class from the PIL Image module does not really have an open method.
But there is an open function in the Image module of the PIL library.
60eca2401a96c192798957.png

A
Artem Imaev, 2021-07-15
@AIRC24

from PIL import Image
img  = Image.open("test.jpg")
img.save("test_good.jpg")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question