-->

Ubuntu Tkinter installation does not include PyIma

2020-08-19 09:44发布

问题:

For those who need to know, I'm running a 64 bit Ubuntu 12.04, and trying to run the problematic script using a pip-installed python3.2

For a project I was writing I wanted to display an image in a tkinter window. To do this I installed Pillow via pip and installed tkinter for python 3 like so:

pip-3.2 install pillow #install stuff here
sudo apt-get install python3-tk

I then tried to run the following script

import tkinter
from PIL import Image, ImageTk

root = tkinter.Tk()
i = Image.open(<path_to_file>)
p = ImaageTk.PhotoImage(i)

There's more, but that block throws the errors. Anyways, when I try and run this I get the following error output

/usr/bin/python3.2 "/home/anish/PycharmProjects/Picture Renamer/default/Main.py"
Traceback (most recent call last):
  File "/usr/local/lib/python3.2/dist-packages/PIL/ImageTk.py", line 184, 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 "/home/anish/PycharmProjects/Picture Renamer/default/Main.py", line 26, in <module>
    n = Main("/home/anish/Desktop/Images/")
  File "/home/anish/PycharmProjects/Picture Renamer/default/Main.py", line 11, in __init__
    self.PictureList = self.MakeImageList(self.dir)
  File "/home/anish/PycharmProjects/Picture Renamer/default/Main.py", line 21, in MakeImageList
    tkim = ImageTk.PhotoImage(temp_im)
  File "/usr/local/lib/python3.2/dist-packages/PIL/ImageTk.py", line 123, in __init__
    self.paste(image)
  File "/usr/local/lib/python3.2/dist-packages/PIL/ImageTk.py", line 188, in paste
    from PIL import _imagingtk
ImportError: cannot import name _imagingtk

Process finished with exit code 1

No amount of googling has given me a solution- the topics I find concerning this usually say to reinstall tkinter and/or pillow.

Here are the contents of my /usr/lib/python3.2/tkinter/

['dialog.py', 'scrolledtext.py', 'simpledialog.py', 'tix.py', 'dnd.py', 'ttk.py', '__main__.py', '_fix.py', 'font.py', '__pycache__', 'messagebox.py', '__init__.py', 'commondialog.py', 'constants.py', 'colorchooser.py', 'filedialog.py']

And here are the [many] files within my /usr/local/lib/python3.2/dist-packages/PIL/

['OleFileIO.py', 'ImageFileIO.py', 'ImageCms.py', 'GimpGradientFile.py', 'PSDraw.py', 'ImageDraw2.py', 'GimpPaletteFile.py', 'TiffImagePlugin.py', 'ImageChops.py', 'ImageShow.py', 'ImageStat.py', 'FliImagePlugin.py', 'ImageColor.py', 'XpmImagePlugin.py', 'ImageOps.py', 'ExifTags.py', 'FpxImagePlugin.py', 'PngImagePlugin.py', 'ImageFile.py', 'WalImageFile.py', 'PixarImagePlugin.py', 'PsdImagePlugin.py', '_util.py', 'ImageDraw.py', 'GribStubImagePlugin.py', 'ContainerIO.py', 'CurImagePlugin.py', 'JpegPresets.py', '_imagingft.cpython-32mu.so', '_imagingmath.cpython-32mu.so', 'PpmImagePlugin.py', 'BmpImagePlugin.py', 'XbmImagePlugin.py', 'DcxImagePlugin.py', 'PaletteFile.py', 'SunImagePlugin.py', 'BufrStubImagePlugin.py', 'JpegImagePlugin.py', 'SpiderImagePlugin.py', 'ImageEnhance.py', 'TgaImagePlugin.py', 'IcnsImagePlugin.py', 'MspImagePlugin.py', 'ImageSequence.py', 'GifImagePlugin.py', 'ImageTransform.py', 'FontFile.py', 'GbrImagePlugin.py', 'EpsImagePlugin.py', 'XVThumbImagePlugin.py', 'BdfFontFile.py', 'PcdImagePlugin.py', 'TarIO.py', 'FitsStubImagePlugin.py', 'ImageMode.py', 'ArgImagePlugin.py', 'IcoImagePlugin.py', '_imaging.cpython-32mu.so', 'McIdasImagePlugin.py', '_binary.py', '__pycache__', 'ImageQt.py', 'Hdf5StubImagePlugin.py', 'PalmImagePlugin.py', 'ImagePalette.py', 'WebPImagePlugin.py', 'ImageFont.py', 'ImagePath.py', 'TiffTags.py', 'ImImagePlugin.py', 'ImageWin.py', 'ImageFilter.py', '__init__.py', 'SgiImagePlugin.py', 'ImageTk.py', 'ImageMath.py', 'GdImageFile.py', 'WmfImagePlugin.py', 'PcfFontFile.py', 'ImageGrab.py', 'PdfImagePlugin.py', 'IptcImagePlugin.py', 'ImtImagePlugin.py', 'MpegImagePlugin.py', 'MicImagePlugin.py', 'Image.py', 'PcxImagePlugin.py']

Can you guys help with this? I'm totally unsure, this has been confusing me for a few days. I'm thinking that MAYBE Ubuntu's python3-tk package is incomplete, but I cant see that being the case. The same goes for pip's pillow. Any ideas?

回答1:

So after posting an issue on GitHub I was told I was missing some libraries.

Specifically I needed to

sudo apt-get install tk8.5-dev tcl8.5-dev

and then

pip install -I pillow

to rebuild pillow. This worked on my raspberry pi running rasbian



回答2:

I struggled with this for a long time. None of these solutions worked for me, other people were hostile claiming the problem had already been solved while pointing to python2.7 instead of python3 or that I must not be following the instructions. But on Ubuntu with more than one computer, I had this problem and here's how I finally solved it:

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

So basically the apt version of turning it off and turning it on again. :/



回答3:

I don't have the rep to comment, so I'll answer instead

I too was getting the error can't from PIL import _imagingtk using python3 on Linux Mint 17 when trying to do a tk_im = ImageTk(im)

First I installed the tk8.6-dev and tcl8.6-dev as suggested above

Then I tried the pip3 --upgrade route, which didn't fix the problem

If I imported PIL in a shell, PIL.PILLOW_VERSION was 2.7.0. Using pip3 list however, it claimed to be 2.3.0.

so I did sudo pip3 uninstall pillow, followed by sudo pip3 install pillow

Now, both methods of reading the version return 2.7.0 and the program works!

When/how I caught pillow 2.3, why it wouldn't upgrade with pip, and why it showed as 2.7 in the python shell I don't know, but the uninstall/install fixed it.