31

I am trying to load in an image from the same folder as the one my python script is in.

# create a class called Person
# create init method
# 2 attributes (name, and birthdate)
# create an object from the Person class

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

# create frame
window = tk.Tk()

# create frame geometry
window.geometry("400x400")

# set title of frame
window.title("Age Calculator App")



# adding labels
name_label = tk.Label(text="Name")
name_label.grid(column=0, row=0)

year_label = tk.Label(text="Year")
year_label.grid(column = 0, row = 1)

month_label = tk.Label(text="Month")
month_label.grid(column = 0, row = 2)

day_label = tk.Label(text="Day")
day_label.grid(column = 0, row = 3)


# adding entries
name_entry = tk.Entry()
name_entry.grid(column=1, row=0)

year_entry = tk.Entry()
year_entry.grid(column=1, row=1)

month_entry = tk.Entry()
month_entry.grid(column=1, row=2)

day_entry = tk.Entry()
day_entry.grid(column=1, row=3)


def calculate_age():
    year = int(year_entry.get())
    month = int(month_entry.get())
    day = int(day_entry.get())
    name = name_entry.get()
    person = Person(name, datetime.date(year, month, day))
    text_answer = tk.Text(master=window, wrap=tk.WORD, height=20, width=30)
    text_answer.grid(column= 1, row= 5)
    answer = "{name} is {age} years old!".format(name=person.name, age=person.age())
    is_old_answer = " Wow you are getting old aren't you?"
    text_answer.insert(tk.END, answer)
    if person.age() >= 50:
        text_answer.insert(tk.END, is_old_answer)


calculate_button = tk.Button(text="Calculate Age!", command=calculate_age)
calculate_button.grid(column=1, row=4)


class Person:
    def __init__(self, name, birthdate):
        self.name = name
        self.birthdate = birthdate

    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year
        return age

image = Image.open('LockVectorDaily.jpg')
image.thumbnail((100, 100), Image.ANTIALIAS)
photo = tk.PhotoImage(file=image)
label_image = tk.Label(image=image)
label_image.grid(column=1, row=0)


window.mainloop()

I got

from PIL import Image, ImageTK 
ImportError: cannot import name 'ImageTK' 

Thank you in advance for the help!

j_4321
  • 15,431
  • 3
  • 34
  • 61
Cody Caro
  • 339
  • 1
  • 3
  • 5
  • How did you install PIL? There’s something wrong with the installation. – Taku Jun 29 '17 at 23:03
  • 2
    The "k" is lower case, ImageTk. Caps/lower case get a lot of people in Tkinter/tkinter. –  Jun 30 '17 at 02:35

10 Answers10

61

For Debian/Ubuntu:

Python 2

sudo apt-get install python-imaging python-pil.imagetk

Python 3

sudo apt-get install python3-pil python3-pil.imagetk

For Archlinux:

sudo pacman -S python-pillow  

It will install the package and you can use it: from PIL import ImageTk

medyas
  • 1,166
  • 13
  • 21
  • 1
    This is for Debian/Ubuntu users. I think the `ImageTk` part is included in the windows version of the `Pillow` module (fork of PIL). For Archlinux users, the install command is `sudo pacman -S python-pillow`. – j_4321 Jan 09 '18 at 15:26
  • For some reason `apt` can always handle this dependency BS itself whereas Python cannot – Jortstek Oct 23 '21 at 20:06
8

For Python3 on Ubuntu 18 I had to uninstall the Python (2) packages then install the Python 3 packages:

apt-get remove python3-pil python3-pil.imagetk python-pil.imagetk python-pil
apt-get install python3-pil.imagetk # Note that python3-pil installed as a dependency
Dan Anderson
  • 2,265
  • 1
  • 9
  • 20
  • 2
    For me, installing `python3-pil.imagetk` was enough. – rjh Dec 16 '19 at 10:58
  • The PIL version supplied by `apt install python3-pil` is (5.1.0), i.e. is dated, and I did not find `python3-pil.imagetk` is installed in Ubuntu 18.04. Note that PIL and [Pillow](https://pillow.readthedocs.io/en/stable/installation.html) cannot coexist, and Pillow should be used instead, and it is version 7 presently. So after uninstalling PIL via the `sudo apt remove` command, you should install PILLOW via `python3 -m pip install --user Pillow`. It will also ensure that `ImageTk` can be imported. – Sun Bear Mar 01 '20 at 20:58
  • The first command deleted some unnecessary things please use it with caution – Meric Ozcan Nov 29 '20 at 09:35
5

Issue is that you named it wrong.

ImageTK instead of ImageTk

keep the k small, and it will work.

Bart
  • 9,825
  • 5
  • 47
  • 73
Mohsin
  • 59
  • 1
  • 2
4

I tried this to install Pillow itself and it works well, i didn't use sudo.

$ pip install Pillow --user

Source for the main installation guide: here

Feel free to edit my answer/correct me.

SH7890
  • 545
  • 2
  • 7
  • 20
4

You will have to change the code like this:

import PIL
from PIL import ImageTk
from PIL import Image

It should work fine!

4
sudo apt-get install python3-pil.imagetk

It worked for me!

Dharman
  • 30,962
  • 25
  • 85
  • 135
2

Python does act up a little when importing TkImage and Image together. You need to import the PIL first and then import TkImage and Image individually like below-

import PIL
from PIL import TkImage
from PIL import Image

This should work fine. You can also check if the pillow is installed in your system correctly by using command prompt like below-

  1. Open Command Prompt
  2. Type python
  3. Once you are in python, just type :
import PIL
  1. If this command doesn't throw any errors, then you are good to follow the above method of importing TkImage and Image separately.
Alessio
  • 3,404
  • 19
  • 35
  • 48
Shashiraj
  • 79
  • 4
0

I am using python 3.6.9 import tkinter, PIL when I open the tkinter module directly , using dir(tkinter) , I see PhotoImage and Image methods . I check the PIL module, dir(PIL) I don’t see any of the above mentioned methods.

I suppose no need to import PIL ... It’s already in the tkinter module. So you could use tkinter.PhotoImage(...) Or tkinter.Image(...)

0

The simple trick is to write them separately and not on a single line.

 import PIL
 from PIL import TkImage
 from PIL import Image
Happy N. Monday
  • 371
  • 1
  • 3
  • 8
-5

Got it figure out! You must import them separately and not on one line.

from PIL import Image
from PIL import ImageTk

Insead of

from PIL import Image, ImageTk
j_4321
  • 15,431
  • 3
  • 34
  • 61
Cody Caro
  • 339
  • 1
  • 3
  • 5
  • 4
    Both cases are equivalent. It works because you replaced the 'ImageTK' of the question by 'ImageTk' which is the right name. – j_4321 Jan 09 '18 at 15:22