Home > OS >  Python/OpenCV: Save pictures with defined path and name
Python/OpenCV: Save pictures with defined path and name

Time:06-24

would like to save an image in a defined path and with a defined name. The path is fixed, but the name from a variable. With the name alone is no problem as you see above but how can i input the fixed path?

import cv2

img_name = "TEST"

cam = cv2.VideoCapture(0)


cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

while(True):

    ret, frame = cam.read()
    cv2.imshow('preview',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        img_name = "{}.jpg".format(img_name)
        cv2.imwrite(img_name, frame)
        break

cam.release()
cv2.destroyAllWindows()

Thanks and best regards :)

CodePudding user response:

import os
import cv2

img_name = "TEST"

cam = cv2.VideoCapture(0)


cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

while(True):

    ret, frame = cam.read()
    cv2.imshow('preview',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        img_name = os.path.join("your_path_here", "{}.jpg".format(img_name))
        cv2.imwrite(img_name, frame)
        break

cam.release()
cv2.destroyAllWindows()
  • Related