Could not import PILLOW_VERSION from PIL - python

While importing, Python (Anaconda) gives the following error:
ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'
I tried removing pillow and then conda install but the error persists.

Pillow 7.0.0 removed PILLOW_VERSION, you should use __version__ in your own code instead.
https://pillow.readthedocs.io/en/stable/deprecations.html#pillow-version-constant
Edit (2020-01-16):
If using torchvision, this has been fixed in v0.5.0. To fix:
Require torchvision>=0.5.0
If Pillow was temporarily pinned, remove the pin
Old info (2020-01-09):
If using torchvision, there is a release planned this week (week 2, 2020) to fix it:
https://github.com/pytorch/vision/issues/1712#issuecomment-570286349
The options are:
wait for the new torchvision release
use the master version of torchvision (eg. pip install -U git+https://github.com/pytorch/vision)
install torchvision from a nightly, which also requires a pytorch from a nightly version
or install Pillow<7 (eg. pip install "pillow<7")

Downgrade pillow if you don't need latest features
pip uninstall pillow
pip install "pillow<7"
Or for anaconda,
conda install -c anaconda "pillow<7"

I have solved by modifying functional.py and __init__.py which are mentioned in error message.Error.
Modify from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION to from PIL import Image, ImageOps, ImageEnhance, __version__ in functional.py approx line number 5.
Modify PILLOW_VERSION = __version__ = _version.__version__ to __version__ = __version__ = _version.__version__ in __init__.py, approx line no 22.
File path:
functional.py:C:\Users\UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\torchvision\transforms\functional.py
__init__.py:C:\Users\UserName\AppData\Local\Programs\Python\Python37\Lib\site-packages\PIL\__init__.py

Currently using torchvision==0.4.2 and this solved my problem.
Downgrade your pillow to 6.1 and restart your Jupyter notebook.
Use this
conda install pillow=6.1

I found one another good solution:
Install Pillow-SIMD instead of Pillow.
Pillow-SIMD is "following" Pillow. Pillow-SIMD versions are 100%
compatible drop-in replacements for Pillow of the same version.
SIMD stands for "single instruction, multiple data" and its essence is
in performing the same operation on multiple data points
simultaneously by using multiple processing elements.
Homepage:
https://github.com/uploadcare/pillow-simd
Benchmarks:
https://python-pillow.org/pillow-perf/
Install instructions:
$ pip uninstall pillow
$ CC="cc -mavx2" pip install -U --force-reinstall pillow-simd
I checked: it has not such error and works on Pytorch/Torchvision.
You don't need to downgrade packages or change source code.

This is my fix to the error
Error: Could not import PILLOW_VERSION from PIL
OS: Linux 18.0 (LUBUNTU)
Python: 3.6
Resolved the issue by downgrading pillow:
pillow: 7.0.0-py36hb39fc2d_0 --> 6.1.0-py36h34e0f95_0
command: conda install pillow=6.1

The less painful way that worked for me is using alias
from PIL import __version__ as PILLOW_VERSION
Why?
Pillow is forked from PIL 1.1.7
VERSION was removed in Pillow 6.0.0
PILLOW_VERSION was removed in Pillow 9.0.0
Use __version__ instead
When you use some older packages, they try to import PILLOW_VERSION which is no longer available. So you'll need to use alias.

Related

problem in upgrading scikit-image version

On my system spyder version '0.13.1' is installed and I want to use
from skimage.filters import unsharp_mask
the error comes:
ImportError: cannot import name 'unsharp_mask
then I treid to upgrade the version using:
1st passed;
!pip install scikit-image
then I passed:
!pip install --upgrade scikit-image
then still version is same which is '0.13.1'.
What should I do?
The problem is resloved with the help of Anaconda official video tutorial :
https://anaconda.cloud/tutorials/getting-started-with-anaconda-individual-edition?source=install

How to solve "AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"?

I encountered it while executing from object_detection.utils import label_map_util in jupyter notebook. It is actually the tensorflow object detection tutorial notebook(it comes with the tensorflow object detection api)
The complete error log:
AttributeError Traceback (most recent call last)
<ipython-input-7-7035655b948a> in <module>
1 from object_detection.utils import ops as utils_ops
----> 2 from object_detection.utils import label_map_util
3 from object_detection.utils import visualization_utils as vis_util
~\AppData\Roaming\Python\Python37\site-packages\object_detection\utils\label_map_util.py in <module>
25 import tensorflow as tf
26 from google.protobuf import text_format
---> 27 from object_detection.protos import string_int_label_map_pb2
28
29
~\AppData\Roaming\Python\Python37\site-packages\object_detection\protos\string_int_label_map_pb2.py in <module>
19 syntax='proto2',
20 serialized_options=None,
---> 21 create_key=_descriptor._internal_create_key,
22 serialized_pb=b'\n2object_detection/protos/string_int_label_map.proto\x12\x17object_detection.protos\"\xc0\x01\n\x15StringIntLabelMapItem\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\n\n\x02id\x18\x02 \x01(\x05\x12\x14\n\x0c\x64isplay_name\x18\x03 \x01(\t\x12M\n\tkeypoints\x18\x04 \x03(\x0b\x32:.object_detection.protos.StringIntLabelMapItem.KeypointMap\x1a(\n\x0bKeypointMap\x12\n\n\x02id\x18\x01 \x01(\x05\x12\r\n\x05label\x18\x02 \x01(\t\"Q\n\x11StringIntLabelMap\x12<\n\x04item\x18\x01 \x03(\x0b\x32..object_detection.protos.StringIntLabelMapItem'
23 )
AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'
The protoc version I got through pip show protobuf and protoc --version were different. The version in pip was a bit outdated.
After I upgraded the pip version with
pip install --upgrade protobuf
the problem was solved.
These three commands solved it for me:
pip uninstall protobuf python3-protobuf
pip install --upgrade pip
pip install --upgrade protobuf
This can be due to incorrect versions of protobuf and python3-protobuf in your IDE(in my case PyCharm) and in site-packages. Following solved error in my case:
pip uninstall python3-protobuf
pip uninstall protobuf
then finally do
pip install protobuf
Also if you have requirements.txt or setup.py check protobuf version from pip freeze and use the same version in your requirements.txt file
pip freeze
(check protobuf version, and use same in requirements.txt)
protobuf==3.15.6 (in my case pip freeze gave me this version for protobuf)
For others looking into this more recently newer google libraries are using proto plus, a wrapper for python proto messages. Using this helper function worked for me (cred: tobked)
import json
import proto
def proto_message_to_dict(message: proto.Message) -> dict:
"""Helper method to parse protobuf message to dictionary."""
return json.loads(message.__class__.to_json(message))
https://github.com/googleapis/python-memcache/issues/19
After trying many different solutions (i'm working on a Mac) the one that worked for me is to reinstall protobuf using:
PROTOC_ZIP=protoc-3.7.1-osx-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP
As is highlighted in this article
the problem for me was i had libprotobuf at 3.11 and protobuf at 3.15 upgrading libprotobuf fixed it for me.
I got this problem in Anaconda too.
I installed it with:
conda install -c conda-forge streamlit
with the versions protobuf and libprotobuf of 3.11.4
I was not able to update neither protobuf nor libprotobuf.
I recommend to install first "conda install protobuf" and then
conda install -c conda-forge streamlit
I got now the versions protobuf and libprotobuf of 3.14.0 and it works.
The issue of my condition is protoc not install.
step to solve the problem:
check pip3 show protobuf, you will see version of protobuf.
e.q. Version: 3.17.3
goto protobuf release page and install same version of protoc
e.q.
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip
unzip protoc-3.17.3-linux-x86_64.zip
vim ~/.bashprofile and paste PATH=$PATH:/home/YOUR_HOST_NAME/bin
source ~/.bashprofile and echo $PATH to check whether /home/YOUR_HOST_NAME/bin exist or not.
check pip3 show protobuf is equal to protoc --version
If not work, reboot and try again.
This can also happen if you use protoc to generate your language specific bindings file in a different OS than where you import it. For example,
Linux OS
protoc -I=./ --python_out=./ my_module.proto
Mac OS
import my_module_pb2
Downgrade protobuf using pip
Sometimes the upgrade code pip install --upgrade protobuf throwed another error. -
version 3.20 seems to be working properly
pip install protobuf==3.20.*
similar to TypeError: Descriptors cannot not be created directly

attributeerror: module 'cv2.face' has no attribute 'createlbphfacerecognizer'

So i'm doing a little personal project but i keep getting this error when I try to create the recognizer. i have opencv-contrib and everything. Does anyone know whats going on? code posted below
import cv2, os
import numpy as np
from PIL import Image
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
recognizer = cv2.face.createLBPHFaceRecognizer()
it gets caught on that last line. I've tried reinstalling all modules already. Not really sure what else to do. The weird thing is it works on my laptop but not my desktop. They both have the same modules, same python release and running the exact same code.
Had the same problem. Use:
recognizer = cv2.face.LBPHFaceRecognizer_create()
It worked for my program.
Under Windows 7, I was able to resolve the issue by simply uninstalling and re-installing opencv:
pip uninstall opencv-contrib-python
pip install opencv-contrib-python
The recogniser is called by:
recognizer = cv2.face.LBPHFaceRecognizer_create()
There are some missing modules for contributed libraries in the default pip install opencv-python so you need pip install opencv-contrib-python
try this it worked for me
pip install --force-reinstall opencv-contrib-python==4.1.2.30
Try to update your opencv by "python -m pip install opencv-contrib-python"
ps: you have to delete the CV2 repository from the Python rep and then run this command (in the CMD windows) if it doesn't work
Try to use this:
import cv2
import os
import numpy as np
from PIL import Image
# Path for face image database
path = 'dataset'
recognizer = cv2.face_LBPHFaceRecognizer.create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
I had this problem running opencv Version 3.4.1. Here is what I did.
SPECS:
Raspberry pi 3B, OS: Raspbian, Version: 9 (Stretch), Python 3, opencv Version 3.4.1
Check opencv version in python
import cv2
cv2.__version__
1) sudo pip install opencv-contrib-python
*After this I could not import cv2 in python until I installed the following.
2) sudo apt-get update
3) sudo apt-get install libhdf5-dev
4) sudo apt-get update
5) sudo apt-get install libhdf5-serial-dev libqtgui4 libqt4-test
I fixed this issue with two commands:
First: sudo pip3 uninstall opencv-contrib-python
Second: sudo python3 -m pip install opencv-contrib-python==3.3.0.9
This fixed my issues. Hope it helps somebody!
Also, if you are using python2, replace “pip3” with “pip” and “python3” with “python”
Uninstall this package (opencv-python) by command :-
pip uninstall opencv-python
Install the library opencv-contrib python using command :-
pip install opencv-contrib-python
then add or check :-
recognizer = cv2.face_LBPHFaceRecognizer.create()
It will work fine
The problem was in opencv-python library cv2.face is not present so it shows attribute missing problem, so install new lib by uninstalling previous one if installed..
recognizer = cv2.face_LBPHFaceRecognizer.create()
You are using Opencv 3.x, in the new version few modules has been removed. You have two options:
1. Add opencv_contrib module to your existing opencv 3.x version.
Here's the link https://github.com/opencv/opencv_contrib
2.you can use older versions of Opencv. Like opencv 2.4.x
open cmd , then --> pip install opencv-contrib-python
I fixed this issue by installing:
sudo pip install opencv-contrib-python.
Then look for correct format.
python2 is default
Basically the problem is that python3 and python2 have different format of code.
recognizer = cv2.face.LBPHFaceRecognizer_create()
This is the format of python2.
recognizer = cv2.face.createLBPHFaceRecognizer()
This is the format of python3
Even I had this problem when I ran the code using Jupyter Notebook.
If you are running it on Jupyter Notebook , then download it as pyhton(.py) file and try to run it in Anaconda Prompt or Command Prompt. This solved the problem for me.
Thank You.
Installing opencv-contrib-python solved the issue.
I had installed only opencv-python, After Installing opencv-contrib-python, solved the issue.
pip install opencv-contrib-python
If you are using python3 use pip3 instead of pip:
pip3 install opencv-contrib-python
Then you can simply do:
recognizer = cv2.face.LBPHFaceRecognizer_create()
write
pip install opencv-contrib-python --upgrade
if gives error then
write
pip install opencv-contrib-python --user
it will work fine for cv2.face.LBPHFaceRecognizer_create()
There are some missing modules for contributed libraries in the default pip install opencv-python-contrib so you need pip install opencv-contrib-python --upgrade
This will download a 41.5MB file and install that in your python package folder , after that you can check by simply First "import cv2" then typing "print(dir(cv2.face))" that will return a list of attributes in cv2.face which will include "createlbphfacerecognizer"
I am also facing an same issue after installation of these below command in my command prompt.
> pip install opencv-contrib-python
> pip install opencv-contrib-python --user
Facing this below error in jupyter notebook.
Training done ---------------
--------------------------------------------------------------------------- AttributeError Traceback (most recent call
last) ~\AppData\Local\Temp/ipykernel_4424/3540845204.py in
40 labels = np.array(labels)
41
---> 42 face_recognizer = cv.face.LBPHFaceRecognizer_create()
43
44 # Train the Recognizer on the features list and the labels list
AttributeError: module 'cv2' has no attribute 'face'
After i am tried with visual studio code but make sure your running with this below confirmation commands in your command prompt or terminal.
I will suggest you to try in command prompt.
Microsoft Windows [Version 10.0.19043.1348]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Admin>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.__version__)
4.5.5
>>> **recognizer = cv2.face.LBPHFaceRecognizer_create()**
>>> **print(recognizer)**
**<face_LBPHFaceRecognizer 000001F890F3D8D0>**
>>>
After successfully fixed this issue, i have got these below outputs.
on.exe' 'c:\Users\Admin\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher'
'55275' '--' 'c:\Users\Admin\Downloads\Opencv\Faces\face.py'
> Training done ---------------
PS C:\Users\Admin\Downloads\Opencv\Faces> c:; cd 'c:\Users\Admin\Downloads\Opencv\Faces'; & 'C:\Program
Files\Python310\pyth0' '--'
'c:\Users\Admin\Downloads\Opencv\Faces\face_recognization1.py'
> Label = Elton John with a confidence of 0.0
> Label = Elton John with a confidence of 0.0
on.exe'
'c:\Users\Admin\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher'
'55339' '--'
'c:\Users\Admin\Downloads\Opencv\Faces\face_recognization1.py'
> Label = nidhi agarwal with a confidence of 0.0

Anaconda and package installation (pydicom)

I have the following problem.
I have tried to install the pydicom package in python 2.7 using the following command (windows, anaconda setup):
conda install -c conda-forge pydicom
everything seems to work fine, the package seems to be installed.
I type
conda list
and in the list I see
pydicom 0.9.8 <pip>
I open spyder, or pycharm, type
import pydicom
and I get
ImportError: No module named pydicom
I have no idea what am I doing wrong. I went through http://conda.pydata.org/docs/using/pkgs.html and everything seems to be fine.
Please assist.
Since you are using 0.9.8, you actually need import dicom rather than import pydicom.
Due to this confusion, it will be import pydicom in version 1.0.0 and later.
I suggest you either update Python Version > 3.0, so that the output for conda list shows something like this:
(pip install pydicom)
pydicom 1.0.2 <pip>
python 3.6.4 h6538335_1
Now import using:
import pydicom #Preferable
==========================================================
Or install dicom instead of pydicom using:
(pip install pydicom-0.9.8)
pydicom 0.9.8 <pip>
python 2.7.0 h6538335_1
And then, import using:
import dicom
However, I strongly suggest that you install pydicom instead of dicom, since it is the upgraded version.

OSError in Pillow when calling Image.open()

For reference, I am using the Anaconda distribution of Python 3.4 on Windows x64, which includes Pillow automatically. I attempted to import Pillow, and it said that the package wasn't installed. I then used conda install Pillow to install Pillow. Now I have a different error.
I'm trying to use Pillow to open an image so that I can import it into a numpy array later. My code, simplified, is as follows.
import numpy as np
from PIL import Image
image = Image.open('foo.tif')
imageArray = np.array(image)
The error I get in the iPython console is as follows.
OSError: cannot identify image file 'foo.tif'
I've run conda list to see if I ran into the same problem as this SO question. However, I get this snippet in the full list:
patsy 0.2.1 np18py34_0
pillow 2.8.1 py34_0
pip 1.5.6 py34_0
This means that within my Anaconda 3.4 Python installation, there is no conflict between PIL and Pillow.
How do I best go about getting rid of this error?
Issue was in the file. Check integrity of your file before blaming anything else! Used a test tiff from here and it worked fine.

Categories