Home > database >  How to save image in an already existing directory?
How to save image in an already existing directory?

Time:06-24

I have created a new directory named augmented, so I wanted to save opened image by adding a random bounding box. How do I save the edited image in the Augmented directory??

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

CodePudding user response:

If you want to save image in augmented directory. just use this:

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

CodePudding user response:

You can do this with cv2.imwrite(filename, image1).

https://techtutorialsx.com/2018/06/24/python-opencv-saving-an-image-to-the-file-system/

  • Related