Python Opencv Ubuntu not creating Windows - python

I have a strange problem with opencv running on an Ubuntu.
I installed OpenCV from the apt sources. And most of the Examples work fine.
But in my programs, which are working with Mac OS, no windows are created.
The following code is showing a window and an image in this on my Mac but not on my Ubuntu powered machine
import time
from opencv import highgui
if __name__ == '__main__':
highgui.cvNamedWindow('Image', highgui.CV_WINDOW_AUTOSIZE)
highgui.cvMoveWindow('Image', 10, 40)
image = highgui.cvLoadImage("verena.jpg", 1)
highgui.cvShowImage('Image', image)
time.sleep(3)
The code is taken from one of the examples that is actually working on both machines.

The code works if I add a highgui.cvStartWindowThread() call before creating the window.
Now the next question would be why the program works on mac os without starting the windowThread.

For the new binding, I mean cv2. The code is cv2.startWindowThread()

Related

cv2.waitKey(0) blocks the program

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.

How do I get Pillow ImageOps.contain to work when inside VS Code?

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.

Problem displaying image opencv+python using imshow + waitKey

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

Spyder doesn't show image with OpenCV

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!

GdkPixbuf Linux dependencies

GdkPixBuf.pixbuf_get_from_window() does not work on my Linux Manjaro (Cinnamon) machine. The code below (only the screenshot part) did work perfectly to grab a screenshot in my old Manjaro installation, but recently I reinstalled, and now the same code does not work. I even tried the same code in Linux Mint (Cinnamon) but it didn't work there either.
No exceptions occur in the code when I run/debug it with Visual Studio Code, nothing in .xsession-errors and nothing sticks out in the journalctl log.
So I can only guess that some dependency is missing on my new machine that I had installed on my old machine, but without an error message it is hard to find out what is missing. I get no output when I run the application from the terminal. The application works fine, except the screenshot part.
I have installed PyGObject and PyCairo in my virtual environment of course. So there must be another dependecy that I am missing, anyone know what is missing?
[Edit] I don't know if this is a problem with my virtual environment (somehing that I need to pip install) or if it something that is missing on my machine (something that I need to sudo pacman -S). When I had it working on my old machine, I had really just learned about virtual environments and python (still a newbie though), so it might have been working because of something I had installed globally.
from gi.repository import Gdk, GdkPixbuf
class ScreenShotHelper(object):
def __init__(self):
"""Setup the screenshothelper class"""
# Get root window (contains all monitors and windows)
self.root_window = Gdk.get_default_root_window()
def grab_and_scale(self, position, size, requested_size):
"""Grab a screenshot and scale it down to requested size"""
# Get specific area from root window
pb = Gdk.pixbuf_get_from_window(self.root_window,
position.left,
position.top,
size.width,
size.height)
# Scale the image down to the size the user asked for
pb = pb.scale_simple(requested_size.width,
requested_size.height,
GdkPixbuf.InterpType.NEAREST)
return pb

Categories