I have an image that is 6400 × 3200, while my screen is 1280 x 800. Therefore, the image needs to be resized for display only. I am using Python and OpenCV 2.4.9. According to OpenCV Documentation,
If you need to show an image that is bigger than the screen resolution, you will need to call namedWindow("", WINDOW_NORMAL) before the imshow.
That is what I am doing, but the image is not fitted to the screen, only a portion is shown because it's too big. I've also tried with cv2.resizeWindow, but it doesn't make any difference.
import cv2 cv2.namedWindow("output", cv2.WINDOW_NORMAL) # Create window with freedom of dimensions # cv2.resizeWindow("output", 400, 300) # Resize window to specified dimensions im = cv2.imread("earth.jpg") # Read image cv2.imshow("output", im) # Show image cv2.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)
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()
image = cv2.imread("img/Demo.jpg") image = cv2.resize(image,(240,240))
image is now resized. Displaying it will render in 240x240.
In opencv, cv.namedWindow() just creates a window object as you determine, but not resizing the original image. You can use cv2.resize(img, resolution) to solve the problem.
image = cv2.imread("740*411.jpg") cv2.imshow("image", image) cv2.waitKey(0) cv2.destroyAllWindows()
Here, it displays a 100 * 200 resolution image after resizing. Remember the resolution parameter use column first then is row.
image = cv2.imread("740*411.jpg") image = cv2.resize(image, (200, 100)) cv2.imshow("image", image) cv2.waitKey(0) cv2.destroyAllWindows()
Use this for example:
cv2.namedWindow('finalImg', cv2.WINDOW_NORMAL) cv2.imshow("finalImg",finalImg)
Try with this code:
from PIL import Image Image.fromarray(image).show()
Looks like opencv lib is pretty sensitive to parameters passed to the methods. The following code worked for me using opencv 4.3.0:
win_name = "visualization" # 1. use var to specify window name everywhere cv2.namedWindow(win_name, cv2.WINDOW_NORMAL) # 2. use 'normal' flag img = cv2.imread(filename) h,w = img.shape[:2] # suits for image containing any amount of channels h = int(h / resize_factor) # one must compute beforehand w = int(w / resize_factor) # and convert to INT cv2.resizeWindow(win_name, w, h) # use variables defined/computed BEFOREHAND cv2.imshow(win_name, img)