python-3.xwebcam-capture

Specify where to save image taken with webcame Python


So I have a Python application that accesses the built-in webcam on a laptop and takes a picture. But I'm having difficulty specifying the storage location for the picture (in this case on the desktop). The code I have so far is:

import cv2
import time
import getpass
import os

getUser = getpass.getuser()
save = 'C:/Users/' + getUser + "/Desktop"

camera_port = 0
camera = cv2.VideoCapture(camera_port)
time.sleep(0.1)
return_value, image = camera.read()
os.path.join(cv2.imwrite(save, "user.png", image))
del camera

But when I run it I get the following error:

Traceback (most recent call last):
  File "C:/Users/RedCode/PycharmProjects/MyApps/WebcamPic.py", line 13, in <module>
    os.path.join(cv2.imwrite(save, "user.png", image))
TypeError: img is not a numpy array, neither a scalar

How can I specify where to store the image when it is taken?


Solution

  • This line here is where you have a problem.

    os.path.join(cv2.imwrite(save, "user.png", image))
    

    You want to do this

    cv2.imwrite(os.path.join(save, "user.png"), image)
    

    imwrite expects two arguments the file name and the image to be saved.

    The call to os.path.join is building your saved file path.