I need to open images (png, jpg) in a Python + Tkinter program. The PIL seems to be installed correctly (used Pillow).

from PIL import Image, ImageTk from Tkinter import Tk root = Tk() image = Image.open('/home/1.png') photo = ImageTk.PhotoImage(image) label = Label(root, image=photo) label.pack() root.mainloop() 

However, an error occurs:

 Traceback (most recent call last): File "1.py", line 10, in <module> photo = ImageTk.PhotoImage(image) File "build/bdist.linux-x86_64/egg/PIL/ImageTk.py", line 123, in __init__ except: File "build/bdist.linux-x86_64/egg/PIL/ImageTk.py", line 188, in paste raise # configuration problem; cannot attach to Tkinter ImportError: cannot import name _imagingtk 

What could be the problem? The _imagingtk itself is imported normally (I mean "import _imagingtk"), but this does not solve the problem.

PS If there is a simpler way to open an image in the Tkinter window - rekvestiruyu it.

    2 answers 2

    Coped with the problem. For the sake of interest, I looked in PIL/ImageTk.py . And there was a line from PIL import _imagingtk . Replaced it with import _imagingtk , reinstalled Pillow , and it all worked.

    There was another question: why Image from Tkinter "overlaps" Image from PIL ? Those. if you first import the PIL, and then Tkinter, then AttributeError: class Image has no attribute 'open' , and if you import the PIL after Tkinter , then this will not happen.

    • It is necessary to watch. By the way, here's your text edited: from PIL import Image, ImageTk import Tkinter root = Tkinter.Tk () image = Image.open ('a.png') photo = ImageTk.PhotoImage (image) label = Tkinter.Label (root , image = photo, bd = 2, relief = Tkinter.SUNKEN) label.pack () root.mainloop () (drowned for clarity). In general, Tkinter itself can open some files (Tkinter.PhotoImage), but something did not work with Label. And problems with name collisions often arise in different languages, the way is qualified names, for example Tkinter.Tk. - alexlz

    I also did about it.

     #!/usr/bin/env python # -*- coding: utf-8 -*- from Tkinter import * windowMain = Tk() windowMain.geometry('600x600+50+50') im ='Директория\фото' ph_im =PhotoImage(file=im) canv111 = Canvas(windowMain, width=500, height=300) canv111.create_image(1,1,anchor=NW,image=ph_im) canv111.place(x=10,y=10) windowMain.mainloop()