Hi whenever I use the cv2.imshow function all I get is a black rectangle like this:
My code is here, I am using python:
from cv2 import cv2
image = cv2.imread("scren.png")
cv2.imshow("image2", image)
I have tried using different file types as well as restarting my computer. Does anyone know how to fix this? Thanks.
I'll put the answer from #fmw42 here as it solved for me too and I was looking for it for some hours
Add cv2.waitKey(0) after cv2.imshow(...).
I don't know why it solves, but solved for me.
According to the documentation the imshow function requires a subsequent command to waitKey or else the image will not show. I believe this functionality has to do with how highgui works. So to display an image in Python using opencv you will always have to do something like this:
import cv2
image = cv2.imread(r"path\to\image")
cv2.imshow("image", image)
cv2.waitKey(0)
Note about imshow
This function should be followed by a call to cv::waitKey or cv::pollKey to perform GUI housekeeping tasks that are necessary to actually show the given image and make the window respond to mouse and keyboard events. Otherwise, it won't display the image and the window might lock up. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame and wait approximately 25 ms for a key press (suitable for displaying a video frame-by-frame). To remove the window, use cv::destroyWindow.
See also.
Related
I am trying to make a game in python 3 which requires a background image. I already have the image. The problem here is my image looks small compared to the big screen. I don't want to change the size of the screen because then I would have to redo all the coordinates of the other objects on the screen. Is there any way I can increase the size of the background image?
Ps. I'm using Python 3 and on VS code.
Thanks in advance!
This is the picture of what the small picture looks like.enter image description here
In order to increase the size of the image, you would need too increase the resolution.
Assuming that you are using windows, open the png with Microsoft Photos, and click on the three horizontal dots at the top right.
A dropdown menu will open, press the third option from the top labeled "Resize"
After this, press on "Define custom dimensions" and you may manipulate the dimensions of the image as you like.
Then, simply save the resized image, and use it in your project.
you can also do this using CV2 library
import cv2
image=cv2.imread("image.png")
scale_percent=1.5
width=int(image.shape[1]*scale_percent)
height=int(image.shape[0]*scale_percent)
dimension=(width,height)
resized = cv2.resize(image,dimension, interpolation = cv2.INTER_AREA)
print(resized.shape)
cv2.imwrite("output.png",resized)
OR you can use PIL library as well
from PIL import Image
image = Image.open("image.png")
image.save("output.png", dpi=(image.size[0]*1.5,image.size[1]*1.5))
What would the most efficient way of flashing images be in python?
Currently I have an infinite while loop that calls sleep at the end, then uses matplotlib to display an image. However I can't get matplotlib to replace the current image, I instead have to close then show again which is slow. I'd like to flash sequences of images as precisely as possible at a set frequency.
I'm opens to solutions that use other libraries to do the replacement in place.
Using openCV you could use this to iterate through images and show them
for img in images:
# Show the image (will replace previously displayed images
# with the same title)
cv2.imshow('window title', img)
# Wait 1000ms before showing the next image and if the user
# pressed 'q', close the window and stop looping through the dataset
if cv2.waitKey(1000) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
Using a library called PsychoPy. It can guarantee that everything is drawn and allows you to control when a window is drawn (a frame) with the window.frame() function.
I take part in a project in which we are making a sudoku solver. I want to print the image of the solved sudoku grid on the screen while our drawing table is drawing the solution on the paper grid.
But I can't find a way to display an image and my code keeps running.
I have looked into - I think - all of the opencv and matplotlib.pyplot functions to display images but every time the code stops when the image is displayed and continues once the image is closed (plt.show() or using cv2.waitKey()).
So if anyone has an idea of a way to display an image while the python code keeps running, I'd be glad to hear it.
Thanks
The PIL/Pillow Image.show() method will leave your image showing on the screen and your code will continue to run.
If you have a black and white image in a Numpy/OpenCV array, you can make it into a PIL Image and display it like this:
from PIL import Image
Image.fromarray(NumpyImg).show()
If your image is colour, you'll need to go from BGR to RGB either using cv2.cvtColor(...BGR2RGB..) or by reversing your 3rd channel something like (untested):
Image.fromarray(NumpyImg[:,:,::-1]).show()
I am viewing the image on the screen using open cv in python. But the image which I opened with the code is halfway on the screen. But I want you to look full screen. How can I do that? Could you help?
You can use this:
cv2.namedWindow("WindowName",cv2.WINDOW_FULLSCREEN)
cv2.imshow("WindowName",output_image)
cv2.waitKey(0)
First you have to create a namedWindow with full screen property. Property you should use is cv2.WINDOW_FULLSCREEN from documentation.
You can create a window manually and resize it. For example:
cv2.namedWindow("Image",WINDOW_NORMAL);
cv2.resizeWindow("Image",1366,768); //Enter your size
cv2.imshow("Image",frame);
If the image is not display on a full screen, then resize image using command cv2.resize()! Good luck!
I am trying to write a program for image display and face capture. I am somewhat new to python and OpenCV.
Please find the details below:
I am running Python 2.7.5 on win32 on windows XP
Open cv2 version is 3.0.0
For the program,
Images from a predefined folder needs to be displayed for a fixed time of 500 millisecond in random sequence.
The gap between the images should be covered through a black screen, which should come for any random time gap interval between 1000-1500 millisecond.
Face capture of the viewer needs to be done via webcam once image showed, in between the image show, i.e. at the point of 250 millisecond. The output of the face should be stored in another newly created folder each time the program is run.
I have written the code below, but not getting the sequence right with a synchronised face capture with Haarcascade integration(perhaps required).
I also read somewhere that 'camera index' could be involved in this with possibly the value zero assigned to it. What exactly could be its role?
Please assist in this. Thanks in advance.
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Sourceimagepath.jpg', 1)
cv2.startWindowThread()
cv2.namedWindow("Demo")
cv2.imshow("Demo", img)
cv2.waitKey(0)
cv2.destroyAllWindows()