Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'ImageTK'

I cannot seem to figure out how to fix a problem with my code regarding import ImageTK from PIL. I have searched and downloaded Pillow different ways and the error of the code is still the same.

Traceback (most recent call last):
File "8_Age_Calculator_App.py", line 3, in <module>
  from PIL import Image, ImageTK
ImportError: cannot import name 'ImageTK'

These are the import codes of the file

import PIL
from PIL import Image, ImageTK
import tkinter as tk
import datetime

and this is the code that is trying to import the image

main_image = Image.open('/Users/Brenden/Documents/Python_OOP/old-people-
running-illo_h.jpg')
main_image.thumbnail((100,100), Image.ANTIALIAS)
main_photo = ImageTK.Photoimage(main_image)
main_label_image = tk.Label(image=main_photo)
main_label.grid(column=1, row=0)

How may I fix this problem?

like image 279
Brenden Avatar asked Nov 28 '22 08:11

Brenden


2 Answers

Use this command for Python 3

 sudo apt-get install python3-pil.imagetk
like image 101
Saurav Kumar Avatar answered Dec 05 '22 15:12

Saurav Kumar


You have a typo in the module you want to import. The k in ImageTk should be lower case:

from PIL import Image, ImageTk

this should solve your problem

and in your script you have another case typo, PhotoImage is CamelCase:

main_photo = ImageTk.PhotoImage(main_image)
like image 37
Jan Steinke Avatar answered Dec 05 '22 15:12

Jan Steinke