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
Related
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 am running python 2.6.6 and want to install the pytesseract package. After extraction and installation, I can call the pytesseract from the command line. However I want to run the tesseract within python. I have the following code (ocr.py):
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('test.png')))
print(pytesseract.image_to_string(Image.open('test-european.jpg'),lang='fra'))
When I run the code by python ocr.py, I get the following output:
Traceback (most recent call last):
File "ocr.py", line 6, in <module>
print(pytesseract.image_to_string(Image.open('test.png')))
File "/pytesseract-0.1.6/build/lib/pytesseract/pytesseract.py", line 164, in image_to_string
raise TesseractError(status, errors)
pytesseract.TesseractError: (2, 'Usage: python tesseract.py [-l language] input_file')
test.png and test-european.jpg are in the working directory. Can Someone help me running this code?
I have tried the following:
Adjusted the tesseract_cmd to 'pytesseract'
Installed tesseract-ocr
Any help is appreciated as I am trying to solve this problem for hours now.
tesseract_cmd should point to the command line program tesseract, not pytesseract.
For instance on Ubuntu you can install the program using:
sudo apt install tesseract-ocr
And then set the variable to just tesseract or /usr/bin/tesseract.
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).
Hi I am new to Python and am following the Python Image Tutorial.
The following executes with no errors after installing the packages described in the tutorial
import numpy
import scipy
import pylab
import pymorph
import mahotas
from scipy import ndimage
However when I try reading an image
image = mahotas.imread('picture_file.jpg')
I get
image = mahotas.imread('image_file.jpg')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.6/dist-packages/mahotas-0.6.4-py2.6-linux-i686.egg/mahotas/init.py", line 68, in imread
raise ImportError('mahotas.imread dependends on freeimage. Could not find it. Error was: %s' % e)
ImportError: mahotas.imread dependends on freeimage. Could not find it. Error was: mahotas.freeimage: could not find libFreeImage in any of the following directories:
'/usr/local/lib/python2.6/dist-packages/mahotas-0.6.4-py2.6-linux-i686.egg/mahotas', '/lib', '/usr/lib', '/usr/local/lib', '/opt/local/lib'
I tried installing FreeImagePy and can import it with no problems but it does not help. I have tried adding to the Python path using package sys but not help either.
EDIT: I should have mentioned that all the packages except pymorph and mahotas was installed
on my linux box doing '*sudo apt-get install package_name*' while pymorph and mahotas was installed by downloading and doing 'sudo python setup.py install'.
Answer for more recent versions of mahotas
Mahotas itself does not have the functionality to read in images. imread is just a wrapper around one of 3 backends:
mahotas-imread (i.e., https://pypi.python.org/pypi/imread)
FreeImage
matplotlib (which only supports PNG & JPEG)
Thus, you need to install one of the packages above. Freeimage can be installed on Ubuntu as described below.
If you are running on Windows, you may wish to try Christoph Gohlke’s packages.
Original answer (for older versions of mahotas)
You need to install freeimage to be able to use mahotas.imread (everything else will actually work without it, it's an optional dependency). This is not a Python package per se, just a regular library.
I don't know what distribution you are in, but try looking for a freeimage package. On debian/ubuntu, you can just do:
sudo apt-get install libfreeimage3
You will have a file libfreeimage.so in /usr/lib or in a similar place and everything will work.