0
votes

I try to use cv2.imshow() to display images, however I find that the result is padded with blank automatically. The code I use is as following:

import cv2
if __name__ == '__main__':   
    img = cv2.imread('test2.png')
    cv2.imshow('dafasf', img)
    key = cv2.waitKey(0)
    if key == 27: # ESC
    cv2.destroyAllWindows()

the image I used enter image description here

1
Can you try with a bigger image? Maybe it has a minimum window size and your image is smaller than that? - guneykayim
You are right, there is no problem when I try a bigger image. Maybe there is a minimum window size in OpenCV. And we can use cv2.WINDOW_NORMAL tag to manually control the window size according to the following answer. - Xianxu Hou

1 Answers

3
votes

By default imshow creates window object with WINDOW_AUTOSIZE attribute. It means that window size is determined by some frontend (GUI) which OpenCV was compiled with.

Your image are not actually changed, it is just displaying issue.

If you want to manually control window attributes, you can try to create namedWindow with your attributes before imshow call, i. e. something like this:

cv2.namedWindow("window name", cv2.WINDOW_NORMAL)
cv2.imshow("window name", img)

According to documentation there are 3 attributes for window displaying (WINDOW_NORMAL, WINDOW_AUTOSIZE and WINDOW_OPENGL), but with the following simple snippet you can determine your actually supported values (in my version I see WINDOW_FREERATIO, WINDOW_FULLSCREEN, WINDOW_AUTOSIZE, WINDOW_NORMAL, WINDOW_KEEPRATIO and WINDOW_OPENGL):

[i for i in list(cv2.__dict__.keys()) if i[:6] == 'WINDOW']