Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to save image in an already existing directory?

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading