Home > Mobile >  How to get image of specific size using python?
How to get image of specific size using python?

Time:08-24

I am trying to get pictures from my camera using open cv I am using this code to access camera and save images in a folder.

import cv2
i = 0
#i = int(input(" "))
cap = cv2.VideoCapture(0)
while True:
    ret,frame = cap.read()
    frame = cv2.cvtColor(frame,0)
    cv2.imshow("frame",frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.imwrite(r"C:\Users\naman.sharma\Desktop\Image_dataset\nm_result_{}.png".format(i), frame)
        break
cap.release()
cv2.destroyAllWindows()

This code works well but I am getting an image of size (600,480) which is very big for the project I am doing.

Is there any specific way to capture images with a certain size. I am trying to capture picture of (250,200).

Thanks for help in advance.

CodePudding user response:

Some devices support various capture sizes, but it is unlikely that your device will support a 250x200 frame size.

Instead of changing the capture size, you can resize the captured frame using cv2.resize.
The last parameter interpolation controls the resampling method. The available options are listed here.

Usage example (with bicubic interpolation):

ret,frame = cap.read()
resized_frame = cv2.resize(frame, (250,200), interpolation=cv2.INTER_CUBIC) 
  • Related