I am currently working on reading an image and displaying it to a window. I have successfully done this, but upon displaying the image, the window only allows me to see a portion of the
full image. I tried saving the image after loading it, and it saved the entire image. So I am fairly certain that it is reading the entire image.
imgFile = cv.imread('1.jpg')
cv.imshow('dst_rt', imgFile)
cv.waitKey(0)
cv.destroyAllWindows()
Image:
Screenshot:
Looks like the image is too big and the window simply doesn't fit the screen.
Create window with the cv2.WINDOW_NORMAL flag, it will make it scalable. Then you can resize it to fit your screen like this:
from __future__ import division
import cv2
img = cv2.imread('1.jpg')
screen_res = 1280, 720
scale_width = screen_res[0] / img.shape[1]
scale_height = screen_res[1] / img.shape[0]
scale = min(scale_width, scale_height)
window_width = int(img.shape[1] * scale)
window_height = int(img.shape[0] * scale)
cv2.namedWindow('dst_rt', cv2.WINDOW_NORMAL)
cv2.resizeWindow('dst_rt', window_width, window_height)
cv2.imshow('dst_rt', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
According to the OpenCV documentation CV_WINDOW_KEEPRATIO flag should do the same, yet it doesn't and it's value not even presented in the python module.
This can help you
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
In openCV whenever you try to display an oversized image or image bigger than your display resolution you get the cropped display. It's a default behaviour.
In order to view the image in the window of your choice openCV encourages to use named window. Please refer to namedWindow documentation
The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.
cv.namedWindow(name, flags=CV_WINDOW_AUTOSIZE)
where each window is related to image container by the name arg, make sure to use same name
eg:
import cv2
frame = cv2.imread('1.jpg')
cv2.namedWindow("Display 1")
cv2.resizeWindow("Display 1", 300, 300)
cv2.imshow("Display 1", frame)
Related
I am using OpenCV in python and the aim of the code is to display one image after another in the same window without destroying it. For eg. i1.jpg is displayed and then in the same window left arrow is displayed after a pause of 5s.
In my case, i1.jpg is displayed and then the window is destroyed and then i2.jpg is displayed in another window.
Here is the code for the same -
t_screen_time = 5000
import numpy
import cv2
left_arrow = "left_arrow.jpg"
right_arrow = "right_arrow.jpg"
img = cv2.imread(left_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
img = cv2.imread(right_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
The argument in cv2.waitKey() is the time to wait for a keypress and not exactly the time the image is displayed for.
cv::imshow() or cv2.imshow() was not designed to be a complete GUI. It was designed to let you quickly debug and display images/videos.
You can achieve what you need using some form of sleep() in a thread or so. But this is harder to do in python. You could also just edit your code to
t_screen_time = 5000
import numpy
import cv2
left_arrow = r"first/image"
right_arrow = r"second/image"
img = cv2.imread(left_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
img = cv2.imread(right_arrow)
cv2.imshow('image',img)
cv2.waitKey(t_screen_time)
cv2.destroyAllWindows()
by removing the cv2.destroyAllWindows() before displaying the second image. I've tested this code and it works, but if you're looking for more GUI functionality, use something like Qt.
I am attempting to load a .gif image into a canvas. Once loaded the picture is to be resized using the .scale method on the canvas. The image reloads but does not resize.
I am using python 3.6. I tried it in python 2.7 with the same result although on the full implementation rather that the simplified code shown below.
import tkinter as tk
f = "C:\Fanny Poer.gif"
root = tk.Tk()
dis = tk.Canvas(root)
dis.grid()
new_image = tk.PhotoImage(file=f)
id = dis.create_image(500, 500, image = new_image)
dis.scale(id, 0, 0, .1, .1)
root.mainloop()
The image loads but does not resize. What am I doing wrong?
Once loaded the picture is to be resized using the .scale method on the canvas.
You can't do that with the scale method. That method only affects the coordinates of an object. It won't resize an image.
To resize an image you can use the subsample and zoom methods on the PhotoImage instance, or you can use some other library (eg: pillow) to resize the image.
I'm running opencv version 3.1.0 and python 2.7 on Mac OSX 10.9 and want to display a black image on fullscreen. My screen's resolution is 2880x1800.
However when I attempt to do so, there is a large white border on the top of the screen.
Here's my code, note that black.jpg is a 2880x1800 image.
import cv2
img = cv2.imread("black.jpg")
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("window", img)
while True:
key = cv2.waitKey(20)
#exit on ESC
if key == 27:
break
I've also tried to just create a black image manually, using the following code.
import cv2
import numpy as np
img = np.zeros((1800, 2880))
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window", cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("window",img)
cv2.waitKey(0)
I've adjusted the dimensions of the numpy array to make it larger but the border still remains.
Doing some research I've discovered that this may be a bug with opencv. However the solutions only apply to windows operating systems, see the following:
OpenCV window in fullscreen and without any borders
and
How to display an image in full screen borderless window in openCV
If anyone has an idea of how to fix the bug for Macs I can go ahead and rebuild the library. Or if I am doing something incorrectly please let me know. Thanks!
I guess the key is to adapt the image size to the REAL macbook screen resolution. 1800x2880 probably is not the one you are currently adopting.
Find out the display resolution of your macbook at System Preferences -> Display -> Scaled
Code with OpenCV
import cv2
def show_full_screen_image():
while True:
print 'loading images...'
img = cv2.imread('preferred_image.png')
# Note: 900x1440 is the resolution with my MBP
img = cv2.resize(img, (1440, 900), interpolation=cv2.INTER_CUBIC)
cv2.namedWindow("test", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("test", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow("test", img)
key=cv2.waitKey(0)
if key==27: # ESC to exit
break
if __name__ == '__main__':
show_full_screen_image()
The problem in those links is not the presence of a border, but the window's background showing through for some reason. From what I understand, OpenCV's namedWindow actually creates a two windows, one inside the other. The "white lines" are actually the grey background of the parent window. You might be facing the same problem in OSX since openCV creates windows this way.
I solved it in windows by changing the background colour of the parent window through the Windows API, maybe you can try something similar in OSX.
I want to get the photo(thumbnail) of a window which I already have it's handle (hwnd) using python in windows os.
From the link i posted in your question comments, I was able to get a sample working that thumbnails my python interpreter window.
from PIL import ImageGrab, Image
import win32gui
hwnd = 2622054 # My python intepreter window
thumbnailsize = 128, 128
# Set the current window to your window
win32gui.SetForegroundWindow(hwnd)
# Get the size of the window rect.
bbox = win32gui.GetWindowRect(hwnd)
# Grab the image using PIL and thumbnail.
img = ImageGrab.grab(bbox)
img.thumbnail(thumbnailsize, Image.ANTIALIAS)
# Save.
img.save('c:/test/test.png')
My image is too large for the turtle window. I had to enlarge the image because the text I need at each spot overlaps.
How do I Resize the window in python?
It sounds like you have drawn an image, but it has gone outside the borders of the window, so therefore you need to make the window larger to see the entire image.
To resize the window:
setup( width = 200, height = 200, startx = None, starty = None)
This will make your output window 200X200 (which may be too small for you so you'll need to make those numbers larger.)
Here is the URL where I found this information.
TurtleDocs