我已经创建了一个名为augmented的新目录,所以我想通过添加一个随机的边界框来保存打开的图像。我如何保存编辑后的图像在增强目录??
import os
from PIL import Image
import cv2
name = "Augmented"
if not os.path.isdir(name):
os.mkdir(name)
os.chdir(os.getcwd()+"/"+name)
f=os.getcwd()
new_path = f[:-10]
os.chdir(new_path)
img_file = []
EXT = ['.jpeg','.png','.jpg']
path = os.getcwd()
mydir = os.listdir(new_path)
for i in mydir:
k = os.path.splitext(i)
if k[1] in EXT:
img_file.append(k[0]+k[1])
for img in img_file:
image = cv2.imread(img)
image1 = cv2.rectangle(image,(234,43),(45,256), (0,0,254), 2)
cv2.imshow("Image" ,image1)
cv2.waitKey(0)
filename1 = os.path.splitext(img)[0]
ext = '.jpg'
filename = filename1+ext
如果您想将图像保存在增强目录中。就用这个:
name = "Augmented" # As you have already used this variable
final_dir = os.path.join(os.getcwd(), name)
image_file = os.path.join(final_dir, "output.jpg")##you can change image name as you wish
cv2.imwrite(image_file, image1) ## image1 is already defined in your code
您可以对cv2.imwrite(filename, image1)
执行此操作。
https://techtutorialsx.com/2018/06/24/python-opencv-saving-an-image-to-the-file-system/