我創建了一個名為 augmented 的新目錄,所以我想通過添加一個隨機邊界框來保存打開的影像。如何將編輯后的影像保存在 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
uj5u.com熱心網友回復:
如果要將影像保存在擴展目錄中。就用這個:
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
uj5u.com熱心網友回復:
您可以使用cv2.imwrite(filename, image1).
https://techtutorialsx.com/2018/06/24/python-opencv-saving-an-image-to-the-file-system/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/495779.html
