I have recently upgraded my python/opencv for a project to python 3.7 + opencv 4.3.0 and now I have an issue with opencvs imshow. I am running Ubuntu 18.04 and am using conda venvs.
I tried to rerun this piece of code multiple times and half the time it correctly displays the white image, and half the time it displays the image below (1). The printed output (2) is always the same. I tried changing it from waitKey(0) to waitKey(1000) but that doesn't make a difference. Still about half the time a tiny black image is all I see.
Does anybody know how to debug this? I tried looking at Pycharms log files but they dont have any more details. I also tried running it straight from the command line but that gives the same issue. Also tried to remove the environment and created a fresh one, reinstalled opencv and got the same issues. When I create a 3.6 environment I don't have the issue, but that's no longer an option. I need python3.7 for some other packages that don't support certain features in 3.6.
I received a warning that libcanberra gtk was missing, and found that in another post that it could cause issues. So I installed it using sudo apt install libcanberra-gtk-module libcanberra-gtk3-module and the warning went away. Sadly the issue did not...
import numpy as np
import cv2
if __name__ == '__main__':
img = np.ones((255, 255, 3), dtype=np.uint8)*255
print(img.shape)
print(img.dtype)
print(img.min())
print(img.max())
cv2.imshow("i", img)
cv2.waitKey(0)
screenshot of the code + result
console output
I think there is no problem. The left-upper part of the image is white as it is supposed to be. The rest is undefined. I recommend equating green and blue channels to zero and leaving only red channel as 255 in order to make sure that is the case.
I solved it in this way:
if cv2.waitKey(0):
pass
Related
I usually program on PC so I'm pretty new to using Mac python libraries. I would like to detect the color of a pixel at the cursors current location without clicking. On PC, I have a something that works using the below code:
pyautogui.moveTo(xcoord,ycoord,1)
x,y=pyautogui.position()
RGBVal=pyautogui.pixel(x,y)
A lot of the tools I have found when searching bring up a new hud that reads out the rgb value. This wont work as the program will move the mouse on the screen and click based on the colors it sees so I can't have the display window on top. The 2 leads I had that were somewhat promising were PIL where I was trying something like this:
rgb = PIL.ImageGrab.grab().load()[pilx,pily]
The issue here is that the coordinates weren't lining up. I tested with a pure red window and it didn't detect that it was in the square until I had the y value at ~3 times the value expected. I was assuming top left corner is 0,0 but tried with inverted y axis and things still weren't lining up properly.
The other attempt I had was using autopy. I was originally having install issues because I was running on python version 3.10. I have downgraded and now see Python 3.7.14 when I use python3 --version. I was able to download and install autopy and when I try again with pip3 it shows "Requirement already satisfied" but when trying to run my code I get a "No module named 'autopy'" error at my "import autopy" line.
If anyone knows how I can make PIL or autopy work or some third option it would be greatly appreciated.
Here is a simple script:
import cv2
img = cv2.imread("img.png", 0)
cv2.imshow("Test", img)
print("Before")
cv2.waitKey(0)
print("After")
After closing the Test window, the output of terminal was like:
$ python test.py
Before
It seems that cv2.waitKey(0) blocks the program and I cannot even kill the process by typing ctrl+c in the terminal. What's wrong with it?
PS: It works properly before but today there is some system upgrade being done and this kind of issue happened. I'm using ubuntu 18.04.
waitKey() is supposed to break/return once all windows have been closed, and also return immediately if no windows are currently open. This is regardless of the desired amount of delay.
On Windows 10, I can't reproduce what you describe. That is to say, it's probably specific to your environment.
Your described behavior is probably a bug due to bad interaction with a window manager/GUI toolkit.
Please check https://github.com/opencv/opencv/labels/category%3A%20highgui-gui for any obvious previous reporting.
Before opening a new bug, make sure it's reproducible and comes with a description of the environment that is required to reproduce it. That includes all versions of everything you think is relevant, including OpenCV's version.
After checking issues of opencv, I do find the same problem that has already been posted:https://github.com/opencv/opencv/issues/20822.
It is a bug due to the Qt Gui backend and the issue is still open.
I have VS Code on a Mac.
Pillow is installed and version verified as 8.3.2 via Pip list in the terminal window of VS Code. I have confirmed via the pillow docs that the ImageOps.contain() is part of 8.3.
My problem is that when I use the terminal, type python and run the following, it works perfectly:
from PIL import Image, ImageOps
im = Image.open("images/Barcelona.jpg")
print(im.format, im.size, im.mode)
im = ImageOps.contain(im, (800, 800), method=3)
im.show()
Preview pops right up and shows me the picture.
When I put the exact code into VS Code or build a .py file with Nano, I get an error message which is shown in this image:
I've verified the right version of Python, Pillow, and such. Any help or pointers would be greatly appreciated.
This turns out to be a problem with what I can only call nested environments and/or a conflict between Anaconda and Workspaces. I'm not really sure but when I used the _version import to figure out a) what VS Code thought and then b) figured out the environment Pip was reporting on, I deactivated up one level, upgraded the (base) to the 8.3 version and all ran fine. The knowledge on importing the version variable to see precisely what the code is importing came from the question asked below and was invaluable.
I am just trying to perform the easiest task in opencv library which is opening and showing an image. I was doing it before on windows computer and it was working perfectly, however when I try to perform the same task on my MacBook it doesn't work. The code is simple:
import cv2
img = cv2.imread('/Users/michalsniady/Desktop/IFE/4th semester/Image Processing And Computer Graphics/nature.jpg', 1)
cv2.imshow('window', img)
cv2.waitKey(10000)
cv2.destroyAllWindows()
When I was downloading opencv I used simply pip install opencv-python if I recall correctly. Maybe the version is wrong? I'm using Python 3.7.6
When I run this program it has two outcomes:
- kernel died
- nothing happens
I am using Spyder with Anaconda
Thank you for any help.
I faced somewhat similar issues.
I reinstalled my open CV and then it worked correctly!
We have the following code:
img = Image.open(FileSystemStorage().path(relpath))
coords = [
cd['crop_start_x'],
cd['crop_start_y'],
cd['crop_end_x'],
cd['crop_end_y']
]
cropped_img = img.crop(coords)
cropped_path = "%s-cropped%s" % os.path.splitext(relpath)
tasks.delete_temporary_file.delay(fss.path(relpath))
cropped_img.save(fss.path(cropped_path))
When trying to save the cropped image, we get a strange "Not a valid number of quantization tables. Should be between 1 and 4." exception, just in one of our environments.
The strangest part is that the code might work sometimes even though the crop or the image doesn't change
Has anyone a lead on this?
We are using Pillow 2.8.1, python 2.7.6 and Ubuntu server 12.04
Basically, the problem originated on a conflicting PIL installation in one of the app servers. It was hard to find since they were hiding behind a load balancer, so the error would pop out sometimes
When we issued pip freeze on the console, we found out that in one of the servers we had both PIL and Pillow installed.
Upon removing both of them and reinstalling Pillow, we solved the issue.
Just to make it clear:
pip uninstall PIL
pip uninstall Pillow
pip install Pillow
And then, just restart the web server.
As others have stated, another possible cause is the use of:
import Image
That statement works only for PIL and should be avoided.
We should always use:
from PIL import Image