image - 如何捕获图像并将其保存到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
我想将它们组合成一个代码。有人可以帮我吗?
解决方案
推荐阅读
- kotlinx.coroutines - kotlin 协程 - 什么是默认范围?
- r - 从多个 txt 文件创建语料库
- node.js - node.js 11.1.0 上的 sqlite3 无法打开数据库
- python - 在python中使用正则表达式提取日期
- c++ - 如何将文件中的数据输入到C++中的矩阵中?
- javascript - 你将如何计算这个函数的空间复杂度?
- svg - VS Code 扩展中的 SVG 工具栏图标
- jpa - CriteriaBuilder JPA:子查询/groupby/maxresult中的多选
- reactjs - React - 将大型表单分成多个组件
- reactjs - “TypeError:无法读取未定义的属性‘selectPage’”