![](/img/trans.png)
[英]I am trying to save an image to a file by using opencv python library using cv2.imwrite() function but it shows an error
[英]How to save the capture image file in USB using python opencv function named "imwrite" in Linux
我嘗試在 Linux 環境中使用 Python 將網絡攝像頭圖像的捕獲保存在 USB 中。
“Imwrite”在文件目錄中有效,但在 USB 目錄中無效。
我試過'os'包和路徑。
有沒有其他方法可以做到這一點?
path='/media/odroid/MYUSB/savefolder/'
capture_img=/demo/capture.jpg
image=cv2.imread(capture_img)
cv2.imwrite(os.path.join(path, resave.jpg),image)
整個代碼運行無誤,但是在MYUSB中沒有保存jpg文件
也許你不需要使用os.join()
試試這個:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
savePath = 'output.jpg' #Replace this with your own path say /media/odroid/MYUSB/savefolder/output.jpg
ret, frame = cap.read()
cv2.imwrite(savePath,frame)
如果您想保存整個視頻,請參閱此答案
這是代碼:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
savePath = 'output.avi' #Replace this with your own path say /media/odroid/MYUSB/savefolder/output.avi
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(savePath, fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
while True:
newret, newframe = cap.read()
cv2.imshow('orig',newframe)
out.write(newframe)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
ret, frame = cap.read()
cv2.imwrite(savePath,frame)
cap.release()
out.release()
cv2.destroyAllWindows()
此外,您的代碼有一些問題,這是它的一個固定版本,它應該可以工作:
path='/media/odroid/MYUSB/savefolder/'
capture_img='/demo/capture.jpg' #it seems path should be demo/capture.jpg
image=cv2.imread(capture_img)
cv2.imwrite(os.path.join(path, 'resave.jpg'),image)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.