3

I wanted to start using pillow, but I ran into some problems:
At first, I thought I could simply pip install pillow, so I activated my virtualenv and did exactly that. When it didn't worked, I realized that I need to install some dependencies for pillow (installation). I'm on Ubuntu 14.04. But even after I installed those dependencies and reinstalled pillow, the Code didn't work. Until I tried it outside of the virtualenv and pip installed pillow on my main Python3.4 installation, where my Code suddenly worked.

import tkinter as tk
from PIL import Image, ImageTk


def show(img, text=""):
    root = tk.Tk()
    root.title(text)
    photo = ImageTk.PhotoImage(img)
    image_lbl = tk.Label(root, image=photo)
    image_lbl.image = photo
    image_lbl.pack()
    root.mainloop()


show(Image.open("test.jpg"), text="Test")

Error:

Traceback (most recent call last):
  File "~/Code/Python/venvs/main/lib/python3.4/site-packages/PIL/ImageTk.py", line 176, in paste
    tk.call("PyImagingPhoto", self.__photo, block.id)
_tkinter.TclError: invalid command name "PyImagingPhoto"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/Code/Python/main/test.py", line 15, in <module>
    show(Image.open("test.jpg"), text="Test")
  File "~/Code/Python/main/test.py", line 8, in show
    photo = ImageTk.PhotoImage(img)
  File "~/Code/Python/venvs/main/lib/python3.4/site-packages/PIL/ImageTk.py", line 115, in __init__
    self.paste(image)
  File "~/Code/Python/venvs/main/lib/python3.4/site-packages/PIL/ImageTk.py", line 180, in paste
    from PIL import _imagingtk
ImportError: cannot import name '_imagingtk'
David
  • 624
  • 6
  • 15

2 Answers2

5

So i had this same problem for the last few days and finally got it resolved. I am running Ubuntu 14.04 as well and i believe i am running python 2.7. The code that I was running was the following

from Tkinter import *
from PIL import Image, ImageTk

app_root = Tk()
img = ImageTk.PhotoImage(Image.open("detailedmodel.jpg"))

imglabel = Label(app_root, image=img).grid(row=1, column=1)

app_root.mainloop()

This was generating the error:

ImportError: cannot import name _imagingtk

I tried a few different things to solving this error based on other solutions online, generally just uninstalling and installing pillow with different developer libraries but the script kept crashing with the same error. Finally I found that in the terminal entering:

sudo pip2.7 install -I --no-cache-dir Pillow

seemed to solve the problem. With the other installs I guess I was working with the wrong version of pillow for python 3 not 2.7.

Hope this helps but it looks like you might have solved the problem already.

user3754203
  • 139
  • 4
  • 8
0

Delete PIL and Pillow packages in lib-packages in your python directory: I am using Conda Env, so

conda remove PIL

and

conda remove pillow

or Delete them directly in lib-packages dir (Suggestion: Make a backup folder).

then, install pillow in this site : http://www.lfd.uci.edu/~gohlke/pythonlibs/#psycopg. If you using Windows Platform install PIL this site : http://www.pythonware.com/products/pil/ (choose based your python version).

Khalif21
  • 97
  • 2
  • 11