How to plot an image using PIL or Matplotlib - matplotlib

I am trying to plot an image in python using either PIL or Matplotlib. I have tried both
import matplotlib.pyplot as plt
and
from PIL import Image
But I get the same error:
ImportError: cannot import name '_imaging' from 'PIL'
I have updated pillow and matplotlib packages but no success

You can read the image using open cv and then plot it using matplotlib.
img_name = cv2.imread("image.jpeg")
plt.imshow(img_name)
plt.show()

This usually happens when folk use python and pip from different packages, distributions or versions. I mean you install a package with pip into one package/distribution/version and then try and use it from a different package/distribution/version of Python.
So, if you normally start Python using:
python
then run:
type python
Whereas if you normally start Python using:
python3
then run:
type python3
On my machine I get:
type python3
python3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
so now I know which Python I am using.
Now you need to see which pip you are using. So, if you normally start pip with:
pip
then run:
type pip
Whereas if you normally start pip with:
pip3
then run:
type pip3
On my machine I get:
type pip3
pip3 is /Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
So now you can see that my pip3 is installing packages for python3 exactly where it can find them.

Related

ModuleNotFoundError: No module named 'numpy' AWS SageMaker Studio Lab

I installed the numpy with Conda, pip and pip3, also tried installing from the requirements.txt file but I am still getting the following issue. Has anyone encountered this before and could please kindly suggest something?
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_732/1818554654.py in
1 import os
----> 2 import numpy as np
3 import matplotlib.pyplot as plt
4 import pandas as pd
5 import random
ModuleNotFoundError: No module named 'numpy'
numpy is built-in the default:Python environment in Studio Lab. If you open a new notebook with the File -> New -> Notebook, and choose the default:Python kernel, you should be able to import numpy without having to install the package.
If you would like to have a reproducible environment with a set of packages, see this sample repo on creating custom environments.
Numpy isn't preinstalled as a module you will need to install it yourself using pip or conda. Using notebooks just include a code block with the following:
!python -m pip install numpy
Suggestion to compile a list of required modules in a 'requirements.txt' file and install using:
!python -m pip install -r requirements.txt
Just as an FYI, the base package list is as follows:
Python 3.9 bzip2 build-essential curl git libgl1-mesa-glx nano rsync
unzip wget ca-certificates pip ipykernel-6.4
Have a look at this link for more info.
SageMaker - Manage your environment

ModuleNotFoundError after installing from github

I installed the OSMNX package from GitHub with pip using
pip install git+git://github.com/gboeing/osmnx.git
and I confirmed OSMNX was installed as it showed up on pip list.
However, when trying to import osmnx I receive an error that NumPy cannot be found. I definitely have NumPy installed, and I confirmed that NumPy shows up in pip list, so I'm not sure why OSMNX can't find NumPy. Any ideas on how to make OSMNX recognize NumPy?
Here's the full error

Pandas incompatible with numpy

I am using anaconda 3. When I try to import pandas I receive the following message:
ImportError: this version of pandas is incompatible with numpy < 1.15.4
your numpy version is 1.15.3.
Please upgrade numpy to >= 1.15.4 to use this pandas version
Printing numpy.__path__ gives me the following
['C:\Users\andrei\AppData\Roaming\Python\Python37\site-packages\numpy']
In conda list, my numpy version is 1.19.1. I checked the above directory to find that it has only numpy 1.15.3 inside and nothing else. Spyder is using this path instead of the anaconda's path to numpy for some arcane reason.
Looks like you have somehow installed several versions of NumPy. Try to remove them all by running several times conda remove numpy and pip uninstall numpy. If you have two versions, the corresponding uninstall command needs to be run twice. After these, install a fresh version of NumPy conda install numpy
You can verify if you still have a version of NumPy installed
conda list | grep numpy
pip list | grep numpy
Note that these commands show only one version number even if you have several copies installed.
You can use conda to upgrade to upgrade your numpy. Run this command in the terminal:
conda update numpy
You need to remove this directory
C:\Users\andrei\AppData\Roaming\Python\
to fix this problem. It seems at some point you used pip to install numpy and that's interfering with the packages installed by conda (which is reporting the right version, as you said).
Furthermore, please be aware that pip and conda packages are binary incompatible, so you should avoid as much as possible to mix them.

Can't download numpy and tensorflow to python 3.5

I have been trying to download tensorflow, and numpy so that they will work when I run them in python 3.5 idle ( the newest model that tensorflow seems to work with). I have both python 3.6 and 3.5 downloaded and I can't seem to make anything that I download( numpy so far) apply to anything but 3.6. I'm not sure if this has something to do with the path that I am calling them through command line, but I was wondering if there was an answer to this situation.
1] Using pip package manager
Use pip to install numpy for you : pip install numpy
If you do not have pip you can install it by following command : python get-pip.py
Use pip to install tensorflow for you: pip install tensorflow
2] Using Anaconda or Miniconda (RECOMMENDED)
Install Anaconda or Miniconda
conda is the package manager for Anaconda and Miniconda
Open the Conda prompt
Anaconda comes with numpy package so no need to explicitly install numpy
In case if you have downloaded Miniconda then you have to install numpy I guess. Run the following command to install numpy : conda install numpy
Run the following command to install tensorflow :conda install -c conda-forge tensorflow or simply conda install tensorflow
Anaconda also comes with it's own pip. You can use Anaconda's pip to install tensorflow by running pip install tensorflow
===========================================================================
By any chance if you get an error in the terminal saying that "command" is not recognized as an internal or external command which means you have not specified the absolute path of pip or python or conda
One solution to the above error can be to add the path of python, pip and anaconda as an environmental variable if you are on windows
If you are on Linux you can add the path of python, pip and anaconda to the PATH variable by export PATH=$PATH:/path/to/dir

%matplotlib inline does not show any plot on jupyter notebook

matplotlib.pyplot takes a long time to run and finally no plot shows on the screen
But matplotlib.back_end() gives the following
u'TkAgg'
Can someone suggest a solution?
NOTE: I installed matplotlib using pip install matplotlib
I had a similar problem working with jupyter notebook on a Mac OSX 10.10.5
In the end I fixed it by reinstalling IPython and dependencies with the command
pip install -U --force-reinstall numpy matplotlib pyzmq jinja2 ipython