I have run the same code(with packages I needed) before and it worked, not sure what's happening now. This show the error,
AttributeError: module 'PIL.Image' has no attribute 'Resampling'. Probably it's small issue, but I can't figure it out, I am working in databricks.
I had the same problem. The easy way is use the old version of Pillow.
pip install Pillow==9.0.0
And your code should work.
Note: You can also use
pip install --ignore-installed Pillow==9.0.0
If for some reason, pip is refusing to install it. Note, however, that it can break dependencies, so use it only as a last resort.
I had the same problem and found that I need to replace PIL.Image.Resampling.BICUBIC with PIL.Image.BICUBIC.
I am using pillow version 7.1.2
from PIL import Image
im = Image.open('image.png')
im2 = im.resize((512,512),resample=Image.BICUBIC)
display(im2)
The resampling enums were seemingly added in Pillow 9.1.0 (released 3 days ago) via this pull request.
I would imagine your Databricks environment has a different verison.
I find that forcing the use of a particular Pillow version may break other packages.
Another solution that works for any Pillow version and avoids deprecation warnings is to insert the code:
import PIL.Image
if not hasattr(PIL.Image, 'Resampling'): # Pillow<9.0
PIL.Image.Resampling = PIL.Image
# Now PIL.Image.Resampling.BICUBIC is always recognized.
my way to fix it:
pip install --ignore-installed Pillow==9.3.0
Same happened when I upgraded some module. I just restarted runtime and it helped.
In my case, pip install Pillow==9.1.0 is suitable for my code
Related
I'm not sure why but I always have an issue with importing PIL. No matter how I try to import it, it just never seems to work. I have tried to install it or update it. I have even tried with Pillow but still nothing. any help is greatly appreciated as it's something so little but so annoying. I am using python 3.10 enter image description here
PIL is deprecated, it's a very old library. Its current maintained fork can be installed via pip install Pillow.
I want to build a Ordered Probit model using statsmodel package in python. Used the following code to import:
from statsmodels.miscmodels.ordinal_model import OrderedModel
but getting a following error:
ModuleNotFoundError: No module named 'statsmodels.miscmodels.ordinal_model'
I have updated the package as well but the error persists.
well,u can install this package in this way:
pip install git+https://github.com/statsmodels/statsmodels
pip install --upgrade --no-deps statsmodels worked for me.
I know it is a pretty old discussion board, but I hope my post can be helpful.
I recently ran into the same issue. and solved by doing the following:
pip3 install git+https://github.com/statsmodels/statsmodels. Just like #AudiR8 mentioned. However, if you are using an IDE with python version 3.0 +, use pip3 is better.
Make sure the package is installed in the correct directory then turn off the IDE.
Reopen it and it should be working.
Hope it can be helpful!
An option that helped me was to restore my console's settings to default and then it worked. My IDE in particular was Spyder
You can try use this instead from statsmodels.discrete.discrete_model import OrderedModel
I'm trying to run some old code from gaussian filter when I find out that python launcher gets stuck trying to do the imshow function.
I tried:
Used Matplotlib to display a graph to see if the python launcher was the problem but no, graph showed up fine.
Remove process in between just to have the image read and display in fear that something in my code was breaking the launcher but no success.
Reinstalled opencv-python but no success.
Also saw one question like this in the google search but OP deleted it.
Has anyone encounter this issue or has any fix for this?
Example code:
import cv2 as cv
filename = 'chessboard.png'
img = cv.imread(filename)
cv.imshow('dst',img)
cv.waitKey(0)
OS: MacOS Big Sur (11.0.1)
I resolved the issue with below steps:
Install the anaconda.
Install the libraries needed.
Run the script, there is an error appeared as below:
You might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
then I installed two libraies:
pip install opencv-python opencv-python-headless
Retry run the script, the image can be shown on the left top of the monitor.
I was also facing the same problem. I solved it by just installing opencv-python-headless. use:
pip install opencv-python-headless
I also encounter this problem when I upgraded to Big Sur.
Uninstall anaconda(every package), and reinstall python.
pip install opencv-python opencv-python-headless
This does help me with imshow() but I can't run cv.face. This attribute is not found.
This solve my problem
pip install --force-reinstall opencv-contrib-python==4.1.2.30
This downgrade gets my code working again.
I naturally have my environment installed in anaconda.
I had to use the package
opencv-python-headless.
It is now running again.
Also the
cv.imread(...)
method
I also ran into this problem after installing macOS Big Sur. It was not only cv2.imshow() that did not respond but also cv2.namedWindow()
cv2.imread() was working however.
Solution was to install opencv-python-headless as others have said before
I had the same problem as you. I solved it by installing anaconda and using anaconda's virtual environment(PyCharm)
import cv2 as cv
import matplotlib.pyplot as plt
filename = 'chessboard.png'
img = cv.imread(filename)
cv.imshow('dst',img)
cv.waitKey(0)
I also found that pc hangs up at cv.imread(...).
Nothing was happened.
So I added line 2 import matplotlib.pyplot ...,
the image was displayed.
It seems to be a package reference issue. My solution solves the issue:
re-create conda env
re-install package, incl. opencv-python-headless
This is the snap of the error that I face (Click this link to watch)
This is common problem in Mac, not only in the Big Sur but also in the Catalina and others too.
and I solved this using a single command.
pip install opencv-python-head
And here watch the problem is solved now (Click this link to watch)
I have opencv 3.4.3 installed (using pip3 install opencv-python and pip3 install opencv-python-contrib)
When I run a code containing this line:
sift = cv2.xfeatures2d.SIFT_create()
I got this error:
AttributeError: module 'cv2.cv2' has no attribute 'xfeatures2d'
Is xfeatures2d function not anymore supported by opencv 3.4.3?
The error message you have is related to the fact that the module xfeatures2d does not exist.
It is not directly related to SIFT algorithm nor any algorithm in xfeatures2d (all will send that error).
I suggest you to either reinstall opencv-contrib-python(pip install opencv-contrib-python) or if you are using anaconda or equivalent to re-install the two opencv package from another source repository.
A last option consist to compile the full OpenCV ("regular" + contrib) by yourself if you are comfortable with it.
Hope it helps.
After hours of hair pulling and installing / compiling everything from the scratch, I needed to post this for other people who might be doing the same little stupid mistake I was doing.
If you have both opencv-python and opencv-contrib-python installed in your system and still getting this error, instead of
sift = cv2.xfeatures2d.SIFT_create()
try
sift = cv2.SIFT_create()
EDIT: My answer was incorrect. Please see the other answer below.
The issue stated in my answer was the solution to a different problem (answered here). However, since you're using OpenCV 3.4.3, I believe you will also have that different problem as well. The original text that was in this answer is now on that other answer, since it is the correct answer to that question.
Try this in your conda prompt.
pip3 uninstall opencv-python
pip3 install -U opencv-contrib-python==3.4.2.16
(i'm on mac os 10.8.5)
I'm using Python 3 (through jupyter notebook) and trying to import cv2
I did import cv2 succefully, but when I type im_g = cv2.imread("smallgray.png", 0) I get this error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-5eb2880672d2> in <module>()
----> 1 im_g = cv2.imread("smallgray.png", 0)
AttributeError: module 'cv2' has no attribute 'imread'
I also checked dir(cv2) and I get:
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__'
I guess a lot of functions are missing...
Is it due to a wrong opencv installation ?
Actually I struggled a lot to get opencv and I guess I installed it 'too many times' and different ways via Terminal. (brew, pip)
Should I uninstall opencv and start over ? How can I do this properly ?
Thx in advance
It might be the case that you installed it in the wrong way. In my case as well, I installed OpenCV wrongly so I uninstalled it completely then reinstalled it which caused it to work.
Please notice the sequence of uninstalling and installing:
Uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
Install:
pip install opencv-contrib-python
pip install opencv-python
I was have this problem too, and i solved it, the problem was been in i named my script cv2.py, i'm just rename it to something else.
Reader's problem could be, that a wrong library (cv2 package) has been installed.
I installed opencv-python3 instead of opencv-python for example.
So in case you have PyCharm IDE, go to File->Settings->Project: *->Python Interpreter and see what you have listed there:
Make sure, you have installed opencv-python.
If you have also other similar names, it should be fine - in my case it works.
This might happen if you have named one of your files as 'cv2.py'. So when you 'import cv2', the system accesses your file instead of the actual library. So try renaming cv2.py' to any other name and your code should work fine.
I had the same issue, this turned out to solve the problem:
from cv2 import cv2
im_g=cv2.imread("smallgray.png", 0)
print(im_g)
Check:
brew list | grep opencv
If it doesn't installed then try:
brew install opencv
it works with me:
pip install opencv-python
pip install opencv-contrib-python
source : https://pypi.org/project/opencv-python/
Do cv2.cv2.imread()
This should work in most of the cases. Also, take a look here.
I faced Similar issue,On Ubuntu
I had installed open-cv using the comand:
sudo pip3 install opencv-python3
I Uninstalled opencv, By:
sudo pip3 uninstall opencv-python3
Then reinstalled it Using the following Code:
pip3 install opencv-python
I also faced this problem.
And get a solution by changing the current cv2.py to cv.py(You can name whatever you like except the name of packages in use).
Little Explanations
Saving the current working file as the name of cv2.py, when we try to run the file it imports the current file with import cv2 statement, not actual cv2 module. This whole thing is causing this whole Error.
The main problem is that you have your file or folder named "cv2", it is not permitted in Python. The way to solve is change the name and then your code will be run
The same issue has recently appeared within my code and had no idea of what's happening. Tried different methods such as uninstalling opencv-python.
Coming to the naming of the file that I am working with, I definitely had no problem that might cause a collapse with the name of the library.
I have deleted the opencv-python, cleaned all of the package-related caches, and then reinstalled the opencv-python and opencv-contrib-python. Now, it is working fine and I can say that no issues have appeared yet.
P.S I am using Arch Linux. If you wonder about how to clear caches, try looking at this source. I hope it helps. Keep learning and exploring, thanks!
I personally had to uninstall all previous installations dealing with opencv :
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless
Then I deleted all files that had to do with opencv too in site-package
after the uninstallation, files related to opencv like opencv_contrib_python-4.6.0.66.dist-info remained and caused errors.
Once all these files were deleted I reinstalled opencv:
pip install opencv-python
now it works for me
using cv2 on vscode
i was having this problem but nothing worked. Turns out that i named my file as "cv2.py" so it was importing itself
"Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error."
Perhaps you have just installed opencv-contrib-python and not opencv-python.
Try this out, it worked for me-
First uninstall:
pip uninstall opencv-python
pip uninstall opencv-contrib-python
then install:
pip install opencv-contrib-python==3.4.2.16
pip install opencv-python==3.4.2.16
You can refer here.