OpenCV & Python - Image too big to display
Although I was expecting an automatic solution (fitting to the screen automatically), resizing solves the problem as well.
import cv2cv2.namedWindow("output", cv2.WINDOW_NORMAL) # Create window with freedom of dimensionsim = cv2.imread("earth.jpg") # Read imageimS = cv2.resize(im, (960, 540)) # Resize imagecv2.imshow("output", imS) # Show imagecv2.waitKey(0) # Display the image infinitely until any keypress
The other answers perform a fixed (width, height)
resize. If you wanted to resize to a specific size while maintaining aspect ratio, use this
def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA): dim = None (h, w) = image.shape[:2] if width is None and height is None: return image if width is None: r = height / float(h) dim = (int(w * r), height) else: r = width / float(w) dim = (width, int(h * r)) return cv2.resize(image, dim, interpolation=inter)
Example
image = cv2.imread('img.png')resize = ResizeWithAspectRatio(image, width=1280) # Resize by width OR# resize = ResizeWithAspectRatio(image, height=1280) # Resize by height cv2.imshow('resize', resize)cv2.waitKey()
Use this for example:
cv2.namedWindow('finalImg', cv2.WINDOW_NORMAL)cv2.imshow("finalImg",finalImg)