ImportError: cannot import name 'ImageTK' - python

I cannot seem to figure out how to fix a problem with my code regarding import ImageTK from PIL. I have searched and downloaded Pillow different ways and the error of the code is still the same.
Traceback (most recent call last):
File "8_Age_Calculator_App.py", line 3, in <module>
from PIL import Image, ImageTK
ImportError: cannot import name 'ImageTK'
These are the import codes of the file
import PIL
from PIL import Image, ImageTK
import tkinter as tk
import datetime
and this is the code that is trying to import the image
main_image = Image.open('/Users/Brenden/Documents/Python_OOP/old-people-
running-illo_h.jpg')
main_image.thumbnail((100,100), Image.ANTIALIAS)
main_photo = ImageTK.Photoimage(main_image)
main_label_image = tk.Label(image=main_photo)
main_label.grid(column=1, row=0)
How may I fix this problem?

Use this command for Python 3
sudo apt-get install python3-pil.imagetk

You have a typo in the module you want to import. The k in ImageTk should be lower case:
from PIL import Image, ImageTk
this should solve your problem
and in your script you have another case typo, PhotoImage is CamelCase:
main_photo = ImageTk.PhotoImage(main_image)

Install it using this command.
sudo apt-get install python-imaging-tk

ImageTK is k(small letter) not K (capital letter)
from PIL import Image, ImageTk

To install for python2 type in terminal:
sudo apt-get install python-pil.imagetk
For python3 type:
sudo apt-get install python3-pil.imagetk
To import Image and ImageTk:
from PIL import Image, ImageTk
There's a typo in your script, it will be PhotoImage:
main_photo = ImageTk.PhotoImage(main_image)

Try to write this command in the terminal:
pip install pillow
It will install PIL packages

Related

ModuleNotFoundError: No module named 'PIL', ImageTk

I am on Windows 10 and my python 3 code is as follows:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Images")
root.iconbitmap("C:/Users/[name]/mu_code/GUI Practice/icon.ico")
# my_img = ImageTk.PhotoImage(Image.open("threat.jpg")
button_quit = Button(root, text="Exit Program", command=root.destroy)
button_quit.pack()
root.mainloop()
When I try to run this it gives the following error:
Traceback (most recent call last):
File "c:\users[name]\mu_code\gui practice\images.py", line 2, in
from PIL import ImageTk, Image
ModuleNotFoundError: No module named 'PIL'
I have used pip install to download Pillow and attempted to download it to different locations to fix the problem but could not as the command prompt stated that the module was already downloaded. How can I fix this issue?
Edit:
import Image
or
import ImageTk
doesn't work either.
I have already run pip install Pillow and have one interpreter, being 'Mu'
What you see here as PIL is actually pillow library
from PIL import ImageTk, Image
Run the command:
pip install Pillow
Check out for PIL
Try to use Import Image, and PIL starts working ( it works in my case):
import Image
instead of
from PIL import Image
What I did is:
Open CMD typed "pip install pillow" if you have not yet install pillow
Copied "PIL" folder from
[C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python39\Lib\site-packages]
Then paste it to
[C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python39\Lib]

Pillow can't import

I have a Problem with the Pillow module.
So basically i tried the methods on this website installing it per pip install Pillow and also importing it per from PIL import Image and import PIL none of them worked
I would really appreciate it if someone helped
and thanks for reading. This is my code:
import pyautogui
import pyscreeze
from PIL import Image
lkchat = pyautogui.locateCenterOnScreen("python.png")
print ("lkchat")
I'm getting the following error message:
from PIL import Image
ImportError: No module named PIL
Kitty Star i would like to suggest you that replace PIL with pillow and then install pillow if your code does not run let me know the next error.

How can I Fix an ImportError

So I am stuck on how can I fix the import error for imresize. I installed all the things I needed like installing Pillow but I haven't been able to find a solution. Also the code that I trying to run is from this github link https://github.com/CSAILVision/IBD
Also side note I do not own this code nor was not the original arthor of it. This is for a research project that I am apart of. In addition I try asked the original arthor with a fix but haven't heard back from them at all.
Installing Pillow, Replacing with code: from scipy.misc.pilutil import imread, installing Pillow-3.3.1-cp27-cp27m-win32.whl, Unninstall Pil and installing Pillow, Reinstalling Pillow.
from util.image_operation import *
from PIL import Image
import numpy as np
from imageio import imresize, imread
from visualize.plot import random_color
from torch.autograd import Variable as V
import torch
I expect it to run but print out the image but it doesn't.
Traceback (most recent call last):
File "test.py", line 4, in <module>
from loader.model_loader import loadmodel
File "/home/joshuayun/Desktop/IBD/loader/model_loader.py", line 5, in <module>
from util.feature_operation import hook_feature, hook_grad
File "/home/joshuayun/Desktop/IBD/util/feature_operation.py", line 6, in <module>
from imageio import imresize, imread, imsave
ImportError: cannot import name 'imresize'
If you want to work with the code as is, i would recommend creating a new environment, and installing an old version of scipy (0.19.1 should do the trick). In your new environment, assuming you are using conda, do:
conda install scipy==0.19.1
If you use pip instead:
pip install scipy==0.19.1
Since scipy 1.3.0rc1 resize has been removed.
I had the same problem, I had scipy 1.4. I did the following and it worked -
pip install scipy==1.1.0

Trying to plot an image in python (using scipy and numpy)

I'm trying to run this code:
http://forumbilder.se/H8CIL/skarmavbild-2018-10-08-kl-21-22-19
so I change it to this one:
and then I got this:
http://forumbilder.se/H8CIL/skarmavbild-2018-10-08-kl-21-22-47
What's wrong? I'm a newbie, and fyi, I'm only have anaconda installed to my Python 3.6.6 .
Regards,
--
and in code:
from skimage import data
photo_data = misc.imageio('./wifire/sd-3layers.jpg')
type(photo_data)
and i get this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-c8186ae7b8e9> in <module>()
1 from skimage import data
2
----> 3 photo_data = misc.imageio('./wifire/sd-3layers.jpg')
4
5 type(photo_data)
AttributeError: module 'scipy.misc' has no attribute 'imageio'
So I change it to:
You have to import imageio. You might have to install the library if you don't have it yet (on your terminal):
pip install imageio
(If you don't have pip install yet, you can do on your terminal sudo easy_install pip, see here: How do I install pip on macOS or OS X?)
And then your code to use read the image with imageio:
import imageio
im = imageio.imread('./wifire/sd-3layers.jpg')
type(im)
To display your image, you can use visvis library (https://imageio.readthedocs.io/en/stable/examples.html). Then you total code would be:
import imageio
import visvis as vv
im = imageio.imread('./wifire/sd-3layers.jpg')
vv.imshow(im)

Anaconda python, PIL and imagingtk

Although this is a reasonably well documented problem, I can't find a solution.
I cannot import PIL and ImageTK.
Minimal example:
import Tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
image = Image.open('live.ppm')
photo = ImageTk.PhotoImage(image)
This produces the error:
File "C:\Anaconda\lib\site-packages\PIL\ImageTk.py", line 181, in paste
import _imagingtk
ImportError: No module named _imagingtk
I have tried:
conda install pillow
conda remove PIL
But no luck.
After uninstalling pillow and PIL:
pip install image
Fixed this issue.
Thanks for bringing this up. The ImageTk module is an extra extension that is not part of the default installation of Pillow.
This issue is long-standing: https://github.com/ContinuumIO/anaconda-issues/issues/150
We (Continuum) will investigate adding this module to our build process.
Try to verify the extension of the image, i got the same problem, and when i modified the extension the script worked

Categories

Resources