[英]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.