首页 > 解决方案 > 如何捕获图像并将其保存到python中的特定文件夹中

问题描述

我尝试制作一个循环的python代码来创建一个新文件夹并将从网络摄像头捕获的一系列图像保存到新文件夹中

我能够编写代码来捕获一系列图像:

 #imports openCV for usage
import cv2
import time

#creates an object called camera, of type openCV video capture, using the
# first camera in the list of cameras connected to the computer.
camera = cv2.VideoCapture(1)#0-front, 1-rear
#loop the following indented code 5 times
for i in range(5):
    # ret- boolean regarding whether or not there was a return at all
    # image-each frame that is returned.
    ret, image = camera.read()
    cv2.imshow("image", image)
    #use the openCV method imwrite (that writes an image to a disk) and write an
    # image using the data in the temporary data variable
    cv2.imwrite('image_'+str(i)+'.png', image)
    time.sleep(3) #pause every 3 second
    print("Image_" + str(i))
# deletes the camrea object, we no longer needs it.
del(camera)
print ("Picture taken. ")

和新文件夹的代码

import os #os-function to interact w/ operating system modules
inPath = 'C:\\Users\\person\\Desktop\\ Project\\python\\Image Saving Test'
os.chdir(inPath)# method in Python used to change the current working directory to specified path.
             # It takes only a single argument as new directory path.
#<<<<<<<<<<<<<<<<<<<<<<<<<<***************>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#Create a new subfolder named 'set_#' for each sequence of image and put
    #the subfolder in Image Saving Test folder
for i in range (1,4):
    Newfolder='Set ' + str(i)
    outPath = os.makedirs(Newfolder)# method used to create a directory recursively.
                    # That means while making leaf directory if any intermediate-Lv
                    # directory is missing, os.makedirs() method will create the

我想将它们组合成一个代码。有人可以帮我吗?

标签: imagedirectoryoperating-systempython-3.9webcam-capture

解决方案


推荐阅读