2017-06-12 171 views
0

因此,我有一個Python應用程序訪問筆記本電腦上的內置攝像頭並拍攝照片。但是我很難指定圖片的存儲位置(在這種情況下,在桌面上)。我到目前爲止的代碼是:指定在哪裏保存用webcame拍攝的圖像Python

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 

但是當我運行它,我得到以下錯誤:

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 

我如何指定存儲圖像拍攝的時候嗎?

回答

1

這條線在這裏是你有問題的地方。

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

你想這樣做

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

imwrite需要兩個參數的文件名和保存圖像。

致電os.path.join正在建立您保存的文件路徑。

+0

謝謝,這工作!不能相信這是一個簡單的錯誤 – RedCode

+0

很高興我能幫上忙!在某些時候我們都會發生。 – Colwin

相關問題