4

I am learning tkinter and trying to place an image on a Button, but I cannot get past the import.

My code:

from tkinter import *
from PIL import Image, ImageTk

The error:

No module named 'ImageTk'

Based on a post I saw when researching this I tried sudo apt-get install python-imaging-tk

which says I already have the latest version.

I am on a Raspberry Pi3, with a new install of Raspbian and I did an upgrade and update to be sure.

Any suggestions?

Here is the output of apt-cache show python-pil requested in a comment below:

Package: python-pil
Source: pillow
Version: 2.6.1-2+deb8u3
Architecture: armhf
Maintainer: Matthias Klose <doko@debian.org>
Installed-Size: 1050
Depends: python (<< 2.8), python (>= 2.7~), python:any (>= 2.7.5-5~), mime-support | python-pil.imagetk, libc6 (>= 2.11), libfreetype6 (>= 2.2.1), libjpeg62-turbo (>= 1.3.1), liblcms2-2 (>= 2.2+git20110628), libtiff5 (>= 4.0.3), libwebp5, libwebpdemux1, libwebpmux1, zlib1g (>= 1:1.1.4)
Suggests: python-pil-doc, python-pil-dbg
Breaks: python-imaging (<< 1.1.7+2.0.0-1.1)
Replaces: python-imaging (<< 1.1.7+2.0.0-1.1)
Provides: python-pillow, python2.7-pil
Multi-Arch: same
Homepage: http://python-pillow.github.io/
Priority: optional
Section: python
Filename: pool/main/p/pillow/python-pil_2.6.1-2+deb8u3_armhf.deb
Size: 277856
SHA256: 9f4e54fbe21fde51c13f4c875a09045a05a61abb55db723c4b09c14ce125eb4d
SHA1: 1e0369561c91e3d8fcaced1027f281ffc05fda32
MD5sum: ec0be47a97ef8aea42211eb1c19ce6bf
Description: Python Imaging Library (Pillow fork)
 The Python Imaging Library (PIL) adds an image object to your Python
 interpreter. You can load images from a variety of file formats, and
 apply a rich set of image operations to them.
 .
 Image Objects:
  o Bilevel, greyscale, palette, true colour (RGB), true colour with
    transparency (RGBA).
  o colour separation (CMYK).
  o Copy, cut, paste operations.
  o Flip, transpose, resize, rotate, and arbitrary affine transforms.
  o Transparency operations.
  o Channel and point operations.
  o Colour transforms, including matrix operations.
  o Image enhancement, including convolution filters.
 .
 File Formats:
  o Full (Open/Load/Save): BMP, EPS (with ghostscript), GIF, IM, JPEG,
    MSP, PDF, PNG, PPM, TIFF, XBM.
  o Read only (Open/Load): ARG, CUR, DCX, FLI, FPX, GBR, GD, ICO, IMT, IPTC,
    MCIDAS, MPEG, PhotoCD, PCX, PIXAR, PSD, TGA, SGI, SUN, TGA, WMF, XPM.
  o Save only: PDF, EPS (without ghostscript).
Description-md5: 7fb415befc961c94ce8c999eb6902e95
  • What version of P{ython are you using? – Steve Robillard Jul 14 '17 at 21:19
  • Have you tried this import ImageTk – Steve Robillard Jul 14 '17 at 21:20
  • After installing this sudo apt-get install python-pil python-imaging python-imaging-tk the above import statement works in 2.7.9. Note: the above command does not require from Pil – Steve Robillard Jul 14 '17 at 22:55
5

I've just checked on my own pi and installing python-pillow does not install the extra libraries that I believe are needed (same goes for python-pil if you are using that one).

I think you need to install both: python-imaging and python-imaging-tk as suggested here: http://effbot.org/imagingbook/imagetk.htm

  • Thanks - still not working. I went into terminal and did sudo apt-get install python-imaging which did install some new code, but I still get the same error when I try my code. So it appears that ImageTk is no longer in PIL???? – Glenn Jul 14 '17 at 20:40
  • That is interesting it worked for me. Did you also install the python-imaging-tk module?. I'm using python 2.7.9 and pillow rather than pil. – flynnstone Jul 14 '17 at 20:49
  • Yes I did. It reports python-imaging-tk is already the newest version. How can I see what is in that module (sorry - Linus newbie) – Glenn Jul 14 '17 at 20:57
  • I'm at a loss I'm afraid. Can you run: apt-cache show python-pil from the terminal and copy in the output please. The only thing that come to mind is you might be installing it for Python 2 and trying to import it into Python 3. Can you run python --version as well. – flynnstone Jul 14 '17 at 21:50
  • Thanks for your help. Seems I am running python 2.7.9 although I don't know why. Here is the info you requested: – Glenn Jul 14 '17 at 22:44
1

I am currently using the raspberry pi and I realized that the latest version of python that it uses is python 3 or 3.5, so if you don't download ImageTk for python 3 or higher, it will not work. you can do this by using this code:

sudo apt-get install python3-pil.imagetk
0

I did this:

sudo apt-get install python-imaging python-imaging-tk

and got:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'python-pil.imagetk' instead of 'python-imaging-tk'

and it installed the new library and it worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.