Вы можете сделать свою собственную функцию изменения размера, чтобы увеличить масштаб и сохранить пропорции, а затем сохранить изображение. Я проверил это на своей IP-камере вместо веб-камеры.
Вот функция изменения размера
# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]
# Return original image if no need to resize
if width is None and height is None:
return image
# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
Полный код
import cv2
import time
# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]
# Return original image if no need to resize
if width is None and height is None:
return image
# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
def main(args):
CAMERA_PORT = 0
IMAGEWIDTH = 3840
IMAGEHEIGHT = 2160
#Propriedades de configuracao da camera
# 3 = width da camera, 4 = height da camera
CAMERA_PROP_WIDTH = 3
CAMERA_PROP_HEIGHT = 4
camera = cv2.VideoCapture(CAMERA_PORT)
camera.set(CAMERA_PROP_WIDTH, IMAGEWIDTH)
camera.set(CAMERA_PROP_HEIGHT, IMAGEHEIGHT)
imagePath = "/home/barbosa/Documents/camera-controller/images/image.png"
while(True):
retval, image = camera.read()
cv2.imshow('Foto',image)
k = cv2.waitKey(100)
if k == 27:
break
elif k == ord('s'):
image = maintain_aspect_ratio_resize(image, width=IMAGEWIDTH)
cv2.imwrite(imagePath,image)
break
cv2.destroyAllWindows()
camera.release()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))