Unable to locate freeimage after install of mahotas in Python - python

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.

Related

How to install scikit-image when the installation appears broken

I'm a newbie in Python, coming from R, and I'm not a programmer, so please be patient if my question is quite silly or trivial, but I cannot find a solution by myself.
I'm using Python 3.7 in Spyder, in a Windows 10 machine.
I'm following this tutorial, and I've understood that I need the scikit-image module.
I've learned that external package should be imported in the same environment of Spyder, so I'm installing them from Spyder:
! pip install scikit-image
And I got this:
Requirement already satisfied: scikit-image in
c:\users\me\appdata\local\programs\python\python37-32\lib\site-packages
(0.15.0) ...
Same for pillow, networkx, PyWavelets, imageio, decorator, numpy (those I suppose they're the dependencies modules). It seems I've what I need.
So I try the first command of the above mentioned tutorial, that is:
from skimage import data
But the results seems this:
Traceback (most recent call last):
File "", line 1, in
from skimage import data
File
"C:\Users\me\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\skimage__init__.py",
line 124, in
_raise_build_error(e)
File
"C:\Users\me\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\skimage__init__.py",
line 104, in _raise_build_error
%s""" % (e, msg))
ImportError: cannot import name 'geometry' from 'skimage._shared'
(C:\Users\me\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\skimage_shared__init__.py)
It seems that scikit-image has not been built correctly.
But I think that this is important:
Your install of scikit-image appears to be broken. Try re-installing
the package following the instructions at:
https://scikit-image.org/docs/stable/install.html
So I went to that link, but the standard installation is what I tried to do. What am I doing wrong?
Thanks in advance
EDIT1:
I've followed the answer, I've run this:
!pip uninstall scikit-image --yes
Uninstalling scikit-image-0.15.0:
Successfully uninstalled scikit-image-0.15.0
Then
!pip install scikit-image
And the result is that is already satisfied:
Requirement already satisfied: scikit-image in
c:\users\me\appdata\local\continuum\anaconda3\lib\site-packages
(0.15.0)
And same for:matplotlib, networkx, pillow, imageio, PyWavelets, numpy, cycler, kiwisolver, pyparsing,python-dateutil, decorator, six, setuptools
EDIT2:, done in the anaconda prompt, already some package are already satisfied, so I restarted Spyder, and sending:
from skimage import data
Gives a different error:
Traceback (most recent call last):
File "", line 1, in
from skimage import data
File
"C:\Users\me\AppData\Local\Continuum\anaconda3\Lib\site-packages\skimage__init__.py",
line 127, in
from .util.dtype import (img_as_float32,
File
"C:\Users\me\AppData\Local\Continuum\anaconda3\Lib\site-packages\skimage\util__init__.py",
line 1, in
from .dtype import (img_as_float32, img_as_float64, img_as_float,
File
"C:\Users\me\AppData\Local\Continuum\anaconda3\Lib\site-packages\skimage\util\dtype.py",
line 1, in
import numpy as np
File
"C:\Users\me\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\numpy__init__.py",
line 142, in
from . import core
File
"C:\Users\me\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\numpy\core__init__.py",
line 23, in
WinDLL(os.path.abspath(filename))
File
"C:\Users\me\AppData\Local\Continuum\anaconda3\lib\ctypes__init__.py",
line 356, in init
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 it's not a valid application of Win32
(the last line is translated by me) that I've searched, but despite is a rather well known problem, I cannot find a solution.
Proposed solution: creating a new (virtual) environment
From the given outputs, I would suggest creating a new environment to encapsulate all packages for your current development.
For help regarding environments together with anaconda you may want to look here or create them within the gui.
Some rational for using virtual environments in general you can find in the documentation.
In short: Environments are an easy way to have separated versions of packages for different projects and you separate your development from the system python installation, which may has different packages.
Why virtual environment in your case?
Currently, it looks like a mix of packages from your system installation, paths like
"C:\Users\me\AppData\Local\Programs\Python..."
and installations of anaconda, with paths from
"C:\Users\me\AppData\Local\Continuum\anaconda3\Lib..."
probably this causes your problem (and probably will cause some more).
Looks like something went wrong when you installed. You may need to reinstall it via:
pip uninstall scikit-image
pip install scikit-image
If there are any errors during the last step (save the output), then post it here. That will give a clue as to what's wrong.
pip uninstall scikit-image
conda install scikit-image
it seems that conda will install some other dependency packets.
And it works for me.
For me this worked:-
pip uninstall scikit-image && pip install scikit-image

Scipy Import Error; cannot import name NUMPY_MKL

I used python xy recently with an old lib of scipy (0.15). Because I wanted to use Spherical Voronoi, I had to install a more recent version of scipy. For this I used pip and the unofficial distributions for python modules with wheel. The install worked without error messages. But now, if I try to import scipy by code, I get this error message below. I already unistalled whole python packages so far and instead installed anaconda. But the error message is still the same. I checked my registry (Windows 10) - can't find anything suspicious.
Any idea? Thank you!
test.py
1.12.1
Traceback (most recent call last):
File "test.py", line 11, in <module>
import scipy
File "C:\Users\Boss\AppData\Roaming\Python\Python27\site-packages\scipy\__init__.py", line 61, in <module>
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
ImportError: cannot import name NUMPY_MKL
Your scipy is built with mkl support and requires mkl support in numpy too. Download numpy from the same site you downloaded scipy (probably Christoph Gohlke's builds). Or if you are using Anaconda use Anaconda packages for both.
Updated - Your error is basically that  numpy+mkl (numpy with Intel math Kernel library) so this is because when you have installed the scipy by precompiled archive, which requires  numpy+mkl but installing numpy using pip won't get you that.
This problem can be easy solved by installation for numpy+mkl from whl file from - http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Old - This is due to the improper installation of numpy, I've had this error few days ago and then I had to install the numpy again. So, upgrade pip and then try installing the numpy whl again. It just worked for me.
Get it here -https://pypi.python.org/pypi/numpy
even this has almost everything - http://www.lfd.uci.edu/~gohlke/pythonlibs/

ImportError: No module named pytesseract

I was following this guide https://realpython.com/blog/python/setting-up-a-simple-ocr-server/ and got to the part where I run cli.py python flask_server/cli.py but I get
python cli.py
Traceback (most recent call last):
File "cli.py", line 3, in <module>
import pytesseract
ImportError: No module named pytesseract
How can I solve this ?
I also saw that I have multiple versions of python. I have linux-kali installed with the latest updates.
Something else: he runs the command like python flask_server/cli.py- where is that flask_server located ? I simply ran it like python cli.py(I was in some directory in which I created the file).
Python import errors usually boils down to one of those three cases (whether is is modules you developed; or modules distributed as packages):
You did no install the required package. Googling pytesseracttells me its an OCR that is distributed and installable using Python package manager tool pip by running pip install pytesseract in your favorite shell.
You did install the package, but is is not in your python path.
(Less often) You did install the package, and it is in your python path, but you used a name already in user by Python, and the two are conflicting.
In your case, I strongly think this is the first one. Case 2. and 3. can be assessed by calling python -v your_script.pyas described in this answer.
I had a similar error. So I hope to help people with this kind of problems.
In my case,
I tried to run python code using pytesseract lib on Raspberry pi 3.
$ pip install pillow
$ pip install pytesseract
(followed by https://www.pyimagesearch.com/2017/07/10/using-tesseract-ocr-python/)
and then, I made an example.py to test.
example.py
try:
import Image
except ImportError:
from PIL import Image
from pytesseract import *
print(pytesseract.image_to_string(Image.open('YOUR_IMAGE_PATH')))
and then, when I run this code, I got below error like you.
ImportError: No module named pytesseract
After I saw the #Bertrand Caron's answer, I found a solution.
My problem was package library path.
I also have multiple versions of python, 2.7 and 3.5, like a writer.
When I run command $python --version on linux, the result is Python 2.7.13.
In my case, when I installed a pytesseract package,
it was stored in "/usr/local/lib/python3.5/dist-packages/pytesseract".
And When I ran $python -v example.py, I found the referenced packages path hadn't been same with upper pytesseract package directory.
cf.
installed pytesseract path : /usr/local/lib/python3.5/dist-packages/pytesseract
actual referenced lib path, when running : /usr/lib/python2.7/dist-packages/
So, I copied pytesseract, located in "/usr/local/lib/python3.5/dist-packages/pytesseract" to "/usr/lib/python2.7/dist-packages/"
Then, Solved!
In my case, I was running it in Jupyter so I used this command,
! pip install --user pytesseract
But I forgot to restart the kernal. You need to restart the kernal after installing the pakcage
I had the same error. My solution is
$ pip3 install pytesseract
since I have python 2 and python 3 installed together.

ImportError: No module named _backend_gdk

I am starting to get some insight into interactive plotting with python and matplotlib using pyGTK+. Therefore I took a look at the example given at the matplotlib website.
This is a short exerpt of the Code:
#!/usr/bin/env python
"""
Example of embedding matplotlib in an application and interacting with
a treeview to store data. Double click on an entry to update plot
data
"""
import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk
import matplotlib
matplotlib.use('GTKAgg') # or 'GTK'
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
from numpy.random import random
from matplotlib.figure import Figure
Ones I try to run this Script in the Terminal I get the following error:
Traceback (most recent call last):
File "gtk_spreadsheet.py", line 15, in <module>
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 33, in <module>
from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
ImportError: No module named _backend_gdk
I have python 2.7 and pygtk 2.24 installed.
Can anyone figure out where the error is located? I think it might be connected to some linking issues?
Note that the Debian/Ubuntu package you need is not 'pygtk2-devel' but 'python-gtk2-dev':
sudo apt-get install python-gtk2-dev
should fix this error on these platforms.
This was a symptom of using a pip-installed matplotlib instead of an apt-installed matplotlib on my system, just now. If on Ubuntu/Debian, try:
pip uninstall matplotlib
apt install python-matplotlib
I believe what was happening is that the pip-install didn't build the C extension required for GTK output, but the apt package has the extension prebuilt. I don't have logs from the initial pip install of matplotlib, so I can't confirm that that's what happened.
In addition to Haldean Brown's answer, note that if you really need to use pip you can force it to recompile matplotlib locally and get "the deep magic that setup.py does" with the --no-binary option:
pip uninstall matplotlib
pip install matplotlib --no-binary=matplotlib
This will solve your problem, provided that you already installed gtk2 with sudo apt-get install python-gtk2-dev
As you want to use the GTKAgg backend, Using pip may prove useful in the future to freeze matplotlib at a version where it is supported (the deprecation warning states it will be dropped in 3.0):
pip install matplotlib==2.2.2 --no-binary=matplotlib
Ubuntu 18.04 has a broken matplotlib package. See this bug: https://bugs.launchpad.net/ubuntu/+source/matplotlib/+bug/1785458
You can either compile matplotlib yourself (e.g. with pip) or use the PPA linked from the bug report.
if you are running py2.7 applications, try:
pip2 install matplotlib --no-binary=matplotlib

Scipy, Numpy, Matplotlib Troubles on OSX

I just installed scipy and numpy using homebrew and pip. I did the following:
brew install python
brew install gfortran
easy_install pip
sudo pip install numpy
sudo pip install scipy
numpy and scipy are both easily found in /Library/Python/2.7/site-packages, but when I open a python shell in the terminal and type:
import numpy
import scipy
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
and the same for scipy. What's wrong? Pip seems to think they're installed.
The python shell you are running from the terminal is probably OSX pre-bundled python and not the one you installed (with numpy & scipy).
You can list the current paths using:
import sys
print(sys.path)
It might be a bit overkill, but I once wrote a short guide to install Python 2.7.x in OS X. You can find it here.
The bottom line is that right now you might have two versions of Python installed, and it can be a bit tricky to uninstall only one of them. If you're sure which one is using the right site-packages folder, then delete the other and update your path variables. If not, I'd suggest to follow that guide.

Categories