我想开始使用枕头,但遇到了一些问题:
起初,我以为我可以简单地pip install pillow
,所以我激活了我的 virtualenv 并做到了这一点。当它不起作用时,我意识到我需要为枕头安装一些依赖项(安装 http://pillow.readthedocs.org/installation.html#linux-installation)。我使用的是 Ubuntu 14.04。但即使在我安装了这些依赖项并重新安装了枕头之后,代码也不起作用。直到我在 virtualenv 之外尝试它并且pip
在我的主要Python3.4安装上安装了pillow,我的代码突然可以工作了。
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'