[Python] Resize an image without distortion OpenCV

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Python 3 and latest version of OpenCV. I am trying to resize an image using the resize function provided, but after resizing, the image is very distorted.

    Code:

    import cv2
    file = "/home/tanmay/Desktop/test_image.png"
    img = cv2.imread(file , 0)
    print(img.shape)
    cv2.imshow('img' , img)
    k = cv2.waitKey(0)
    if k == 27:
    cv2.destroyWindow('img')
    resize_img = cv2.resize(img , (28 , 28))
    cv2.imshow('img' , resize_img)
    x = cv2.waitKey(0)
    if x == 27:
    cv2.destroyWindow('img')


    The original image is 480 x 640 (RGB, therefore I passed the 0 to get it to grayscale).

    Is there any way I can resize it and avoid the distortion using OpenCV or any other library perhaps? I intend to make a handwritten digit recogniser and I have trained my neural network using the MNIST data, therefore I need the image to be 28x28.

    Continue reading...

Compartilhe esta Página