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'
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.
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).
Related
I'm trying to use pyautogui's screenshot functions with Python 3.6.5 on OSX 10.11.
>>> import pyautogui
>>> image = pyautogui.screenshot()
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py", line 331, in _screenshot_osx
im = Image.open(tmpFilename)
NameError: name 'Image' is not defined
My understanding is that pyscreeze is failing to get the name Image from Pillow for some reason. I tried to update the pyautogui (it was up to date), then reinstall the pyautogui, which carries all its dependencies including pyscreeze and Pillow along with it.
I found this question with the same issue, but the fix that worked there (reinstalling) isn't working for me.
do
pip install Pillow==0.1.13
since Image is module from PIL
pip3 uninstall pyautogui
pip3 uninstall Pillow
then reinstall the modules and restart you editor.
I am trying to do some studies and automation related to image metadata.
from PIL import Image
Image.open("/Users/carlo/Desktop/JPEG 2/DSC_0393.jpeg")
This is the error that I am receiving:
Traceback (most recent call last):
File "/Users/carlo/PythonProjects/ImageMetaData_00/main.py", line 1, in <module>
from PIL import Image
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL/Image.py", line 114, in <module>
from . import _imaging as core
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL/_imaging.cpython-310-darwin.so, 2): no suitable image found. Did find:
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL/_imaging.cpython-310-darwin.so: mach-o, but wrong architecture
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL/_imaging.cpython-310-darwin.so: mach-o, but wrong architecture
I am using Python 3.10, not sure what I am missing. Thanks!
It’s telling you you’ve got a version of PIL downloaded/installed, but it’s not suitable for your computer architecture. You’re probably on an M1 Mac instead of an Intel one. To fix this try these:
pip3 install wheel
pip3 install --no-cache-dir pillow
If that doesn't work, you can try to switch to using python via Rosetta.
Go to the Application folder -> Right-click on Terminal App -> Get Info
Tick Open with Rosetta option.
Also try reinstalling it: pip3 install pillow.
If all else fails try downgrading python and see if anything clicks.
Pillow was working fine for me and then I got this error :
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 2452, in open
% (filename if filename else fp))
IOError: cannot identify image file 'dataSet/.DS_Store'
From answers on SO I got that it is possible I have both PIL and pillow installed and so I uninstalled PIL and installed pillow using easy_install and ever since then I keep getting this error.
ImportError: No module named PIL
I tried everything.
pip install image
pip install pillow
pip upgrade pip
import PIL from Image
pip --version gives : pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)
pip freeze | grep Pillow gives : Pillow==4.1.0
The code I'm running is:
import os
import cv2
import numpy as np
from PIL import Image
And at line 4 I keep getting the error
Traceback (most recent call last):
File "finder.py", line 4, in <module>
from PIL import Image
ImportError: No module named PIL
What else can I do to fix the situation? Did I read all the posts related to this topic on SO? Pretty much. And there are MANY posts related to it as well,but please,help me.
It seems that You have Installed Pillow for python3.*
pip install pillow
Install the right version for python2.7
from here
https://pypi.python.org/pypi/Pillow/4.0.0
python2.7 version has cp27 in it's name, download the right wheel file based on your system architecture.
pip install some-package.whl
I hope it helps.
Regarding the first error, this is an expected error:
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 2452, in open
% (filename if filename else fp))
IOError: cannot identify image file 'dataSet/.DS_Store'
You're trying to open a non-image with Pillow, and it's responding by saying it cannot open it. This is correct behaviour.
I guess you're trying to open all the images in a directory, but it's failing on .DS_Store, which a a metadata file created by macOS when viewing the contents of the directory. This file won't have existed before you looked in the directory with Finder, hence the code worked. What you need to do is either ignore this file (as you would with Thumbs.db on Windows), be more careful with globbing (eg. make sure only .jpg or .png or such) or handle the IOError with a try:/except IOError: block.
I wrote this code:
import Image
im = raw_input("Insert Image file: ")
handle = Image.open(im)
print handle.size
to read an Image file and print its size, but when I run this code I get a traceback:
Traceback (most recent call last):
File "image.py", line 1, in <module>
import Image
P.S - I wrote the program in mac os x if it matters
You most likely need to install PIL or Pillow. Here is the installation guide which can be summarized as:
Install Brew:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install some dependencies for Pillow:
brew install libtiff libjpeg webp little-cms2
Install Pillow:
pip install Pillow
And change:
import Image
To:
from PIL import Image
If you name your file image.py, and then try to import Image there is a high risk that Python tries to import your own file and that all that ends in a stack overflow (the bad event, not the nice site).
NEVER give you files names that exists in Standard Python Library
I get the following error running the simple python script below:
Traceback (most recent call last):
File "/home/user/Desktop/python/folder/pil_test.py", line 4, in <module>
im = Image.open('screenshot.png')
AttributeError: 'module' object has no attribute 'open'
I run Debian Wheezy with KDE.
I have installed pillow via pip.
The script ran well on my previous system, so I suspect the issue
lies outside of my source code.
I feel slit up and have no idea where to start to solve that issue.
Just reinstalling pillow did not do the job for me.
What else could I do?
from PIL import Image
im = Image.open('screenshot.png')
left = '10'
top = '10'
right = '10'
bottom = '10'
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png')
It appears uninstalling 'pillow' and install 'pil' did the job for me:
sudo pip uninstall pillow
sudo pip install pil
This is strange in so far as I thought installing pillow via pip must have included PIL automatically.
I do not know why. But it works now!