[PYTHON] How to solve the problem that only the process remains when you press cross on the imshow screen of OpenCV

environment

Python 3.7.8  OpenCV 3.4.2

problem

For example, this image of Lena. lena.jpeg

Normally, display the image with the following code and press the appropriate key to close the window. However, when I press the close button, the window closes but the python process remains running.

open.py


import cv2 

img = cv2.imread("lena.jpg ")

#Display image
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Solution

It's basically the same as Other method, but with a little shorter code and everything as before. Originality is also compatible with the key input of.

python


import cv2

img = cv2.imread("lena.jpg ")

while(1):
    cv2.imshow('img', img)
    key = cv2.waitKey(100) & 0xff
    if key != 255 or cv2.getWindowProperty('img', cv2.WND_PROP_AUTOSIZE) == -1:
        cv2.destroyAllWindows()
        exit()

I hope it helps those who are in trouble with the same thing.

Recommended Posts

How to solve the problem that only the process remains when you press cross on the imshow screen of OpenCV
How to solve the problem that time goes wrong every time you turn on the power in Linux
How to solve the problem that video content cannot be played on Firefox for Linux
How to solve the problem that the login screen is not displayed forever on Ubuntu 19.04 because it stops at the logo at startup
How to deal with the error "Failed to load module" canberra-gtk-module "that appears when you run OpenCV
How to solve the bin packing problem
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
How to deal with the problem that build fails when CI / CD of Python Function with AWS Amplify
How to make only one data register on the Django admin screen
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac
Solution to the problem that build does not end when installing OpenCV (PEP517)
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
How to filter foreign keys that can be selected on the Django admin screen
A memo on how to overcome the difficult problem of capturing FX with AI
The 15th offline real-time I tried to solve the problem of how to write with python
How to solve the recursive function that solved abc115-D
How to deal with the problem that pandas 1.1.0 or later build fails on Alpine Linux
How to write offline real time I tried to solve the problem of F02 with Python
How to delete "(base)" that appears in the terminal when Anaconda is installed on Mac
I want to use only the normalization process of SudachiPy
The problem becomes easier to solve depending on the formulation method
Through the installation of pip package that depends on opencv-python when building opencv from source code
[GNOME] How to avoid the problem that conversion candidate characters do not disappear when converting to Japanese
How to unify the bin width when displaying multiple histograms on top of each other (matplotlib)
How to deal with the problem that the current directory moves when Python is executed from Atom
The story of the algorithm drawing a ridiculous conclusion when trying to solve the traveling salesman problem properly
How to judge that the cross key is input in Python3
When you want to save the result of the callback function somewhere
How to save only a part of a long video using OpenCV
How to use Jupyter on the front end of supercomputer ITO
How to update the python version of Cloud Shell on GCP
The story of failing to update "calendar.day_abbr" on the admin screen of django
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib