import cv2 videoCapture = cv2.VideoCapture('test.mp4') cv2.namedWindow('name', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) while True: rval, frame = videoCapture.read() if rval is True: cv2.imshow('name', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break videoCapture.release() cv2.destroyAllWindows()
The key is to set the window property cv2.window | normal | cv2.window | keepratio before cv2.imshow.
PS: I tested it on PyCharm Community Edition, but the professional edition didn't keep the proportion. Maybe the back end of the professional edition is not Qt, but an independent form.
Qt back end support flag:
Window? Normal or window? Autosize
Window? Normal enables you to resize the window, while window? Autosize automatically resizes the window to fit the displayed image (see imshow), and you cannot manually change the window size.
Window? Freeratio or window? Keeratio
Window? Freeratio adjusts the image regardless of its scale, while window? Keeratio keeps the image scale.
Window? GUI? Normal or window? GUI? Expanded
Window GUI normal is an old method of drawing Windows without status bar and toolbar, while window GUI expanded is a new enhanced GUI.
By default, flags = = window | autosize | window | keepratio | window | GUI 〝 expanded
cv2.namedWindow source code annotation
""" namedWindow(winname[, flags]) -> None . @brief Creates a window. . . 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. . . If a window with the same name already exists, the function does nothing. . . You can call cv::destroyWindow or cv::destroyAllWindows to close the window and de-allocate any associated . memory usage. For a simple program, you do not really have to call these functions because all the . resources and windows of the application are closed automatically by the operating system upon exit. . . @note . . Qt backend supports additional flags: . - **WINDOW_NORMAL or WINDOW_AUTOSIZE:** WINDOW_NORMAL enables you to resize the . window, whereas WINDOW_AUTOSIZE adjusts automatically the window size to fit the . displayed image (see imshow ), and you cannot change the window size manually. . - **WINDOW_FREERATIO or WINDOW_KEEPRATIO:** WINDOW_FREERATIO adjusts the image . with no respect to its ratio, whereas WINDOW_KEEPRATIO keeps the image ratio. . - **WINDOW_GUI_NORMAL or WINDOW_GUI_EXPANDED:** WINDOW_GUI_NORMAL is the old way to draw the window . without statusbar and toolbar, whereas WINDOW_GUI_EXPANDED is a new enhanced GUI. . By default, flags == WINDOW_AUTOSIZE | WINDOW_KEEPRATIO | WINDOW_GUI_EXPANDED . . @param winname Name of the window in the window caption that may be used as a window identifier. . @param flags Flags of the window. The supported flags are: (cv::WindowFlags) """