简体   繁体   English

逐个打开图像,绘制矩形,然后保存在另一个文件夹中

[英]Open images one by one, draw rectangle and then save in another folder

I want to open a folder where there are several images, in each image will be drawn a rectangle and saved with the same original name but in another folder 我想打开一个文件夹,里面有几个图像,在每个图像中都会绘制一个矩形并用相同的原始名称保存但在另一个文件夹中

The problem is that images are created but overwritten, and I can not save with the same name automatically. 问题是图像被创建但被覆盖,我无法自动保存相同的名称。 I think I cannot define well the for loop. 我想我无法很好地定义for循环。

import cv2
import glob
path = r'dir\*.tif'  # only .tif images
for file in glob.glob(path):
    im = pl_image(file, size= x)
    cv2.rectangle(im,(x1,y1),(x2,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x3,y1),(x4,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x5,y1),(x6,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x7,y1),(x8,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x9,y1),(x10,y2),(0, 255, 0),2)
    cv2.imwrite('%s/%s.JPEG' %  , im) # this line I do not how to define, should be smt like this

This line works but it does not save with the original name 此行有效,但不保存原始名称

cv2.imwrite(r'other_dir\img.png',im)
import cv2
import glob
path = r'dir\*.tif'  # only .tif images
output_folder_path = "/home/output/"
for file in glob.glob(path):
    im = pl_image(file, size= x)
    output_file_name = output_folder_path+file.split("/")[-1]
    cv2.rectangle(im,(x1,y1),(x2,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x3,y1),(x4,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x5,y1),(x6,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x7,y1),(x8,y2),(0, 255, 0),2)
    cv2.rectangle(im,(x9,y1),(x10,y2),(0, 255, 0),2)
    cv2.imwrite(output_file_name, im)

The Idea is to have a variable that stores the fully qualified file name for the output folder + existing filename Idea是拥有一个变量,用于存储输出文件夹+现有文件名的完全限定文件名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 open cv 将多个图像保存在一个文件夹中? - How to save multiple images in one folder by using open cv? 从一个文件夹中提取图像,对其进行处理并使用 python、opencv 保存到另一个文件夹中 - Extract images from one folder, process it and save into another folder with python, opencv 使用python逐个打开文件夹中的图像? - Open images from a folder one by one using python? 迭代文件夹一次打开,处理和保存图像 - Iterative folder open, process and save image one by one 我需要将所有.xlsx文件保存在一个文件夹中,并保存到另一个文件夹中 - I need to save all .xlsx files in one folder, into another folder 从一个文件夹中分离图像并将它们放在另一个文件夹中 - Seperation of images from one folder and putting them in another folder 如何在python中将备用图像从一个文件夹复制到另一个文件夹 - How to copy alternate images from one folder to another in python 使用 pandas dataframe 上的名称将图像从一个文件夹复制到另一个文件夹 - Copy images from one folder to another using their names on a pandas dataframe 将图像从一个文件夹复制到另一个文件夹时找不到文件错误 - File not found error when copying images from one folder to another 如何修改一个文件夹的多个 .xml 文件并将它们保存到另一个文件夹? - How to Modify Multiple .xml files of one folder and save them to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM