I’m using the version of OpenCV that comes pre-installed with Ubuntu 22.04 LTS. Previously, I set up my OpenCV window with the following configuration:
<code>cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
<code>cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
</code>
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
This worked perfectly: even though the input image is wider than the screen, the image scaled down proportionally, allowing me to view the entire image at once in fullscreen mode (expanding horizontally). Here’s an example of how it used to look:
However, after installing (and later reinstalling) packages like PyQt5
, PyQt5-sip
, etc., the behavior of my OpenCV window changed. Now, instead of expanding the image horizontally, the window stretches it vertically, which distorts the display. Here’s the current (undesired) behavior:
Below is a MRE:
from screeninfo import get_monitors
monitor = get_monitors()[0]
return (monitor.width, monitor.height)
def write_text(image, text="Hello World"):
font = cv2.FONT_HERSHEY_SIMPLEX
font_color = (255, 255, 255)
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
x = (image.shape[1] - text_width) // 2
y = (image.shape[0] + text_height) // 2
cv2.putText(image, text, (x, y), font, font_scale, font_color, font_thickness)
image = np.random.randint(0, 256, (1280, 2560, 3), dtype=np.uint8) # dummy image
image = write_text(image)
window_name = 'Full Screen Image'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
# cv2.resizeWindow(window_name, get_screen_size()) # 1920 x 1200 # This is my workaround. But originally this line was not in the code
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow(window_name, image)
<code>import cv2
import numpy as np
from screeninfo import get_monitors
def get_screen_size():
monitor = get_monitors()[0]
return (monitor.width, monitor.height)
def write_text(image, text="Hello World"):
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 14
font_thickness = 14
font_color = (255, 255, 255)
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
x = (image.shape[1] - text_width) // 2
y = (image.shape[0] + text_height) // 2
cv2.putText(image, text, (x, y), font, font_scale, font_color, font_thickness)
return image
image = np.random.randint(0, 256, (1280, 2560, 3), dtype=np.uint8) # dummy image
image = write_text(image)
window_name = 'Full Screen Image'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
# cv2.resizeWindow(window_name, get_screen_size()) # 1920 x 1200 # This is my workaround. But originally this line was not in the code
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow(window_name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code>
import cv2
import numpy as np
from screeninfo import get_monitors
def get_screen_size():
monitor = get_monitors()[0]
return (monitor.width, monitor.height)
def write_text(image, text="Hello World"):
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 14
font_thickness = 14
font_color = (255, 255, 255)
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
x = (image.shape[1] - text_width) // 2
y = (image.shape[0] + text_height) // 2
cv2.putText(image, text, (x, y), font, font_scale, font_color, font_thickness)
return image
image = np.random.randint(0, 256, (1280, 2560, 3), dtype=np.uint8) # dummy image
image = write_text(image)
window_name = 'Full Screen Image'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
# cv2.resizeWindow(window_name, get_screen_size()) # 1920 x 1200 # This is my workaround. But originally this line was not in the code
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.imshow(window_name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
How to revert the original fullscreen behavior where the image scales horizontally, fitting within the screen while maintaining its aspect ratio?