【发布时间】:2018-05-02 17:05:28
【问题描述】:
我正在从文件夹中读取 .png 图像并进行一些操作,我想按 Img1.png、Img2.png、Img3.png 之类的顺序保存这些图像。我尝试使用此代码:
cv2.imwrite("Img{}.png".format(i),Image)
但它一直覆盖单个图像。
下面是代码:
def main(path):
i=0
image = cv2.imread(path)
#do some operations on image
cv2.imwrite('Img{}.png'.format(i),image)
i=i+1
cv2.waitKey(0)
路径包含多个 .png 图像,因此当 main 执行时它会重置 'i' 的值并覆盖输出。
【问题讨论】:
-
我不知道你程序的复杂性,但你不能为 i 使用全局变量吗?
-
你使用什么模块?
-
我尝试使用全局变量,但显示错误。我使用 numpy 和 opencv 以及 python 版本 2.7.14
-
听起来你想弄清楚目录中已经有哪些图像,然后从下一个可用的图像名称开始索引。