Kuldeep Singh Chouhan
Kuldeep Singh Chouhan

Reputation: 73

How to use imwrite taking images from one folder and saving them to another using Opencv?

I want to augment images inside a folder. I also want to keep the names of images the same after augmentation in a different folder. How can I do this using OpenCV?

# Defining path

INPUT_IMG_DIR = 'NORMAL'
OUTPUT_AUG_DIR = 'AUGMENT'

seq = iaa.Sequential([iaa.Affine(rotate=5)
#                           iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
#                           iaa.Multiply((0.5, 1.5), per_channel=0.5),
#                           iaa.Add((-10, 10), per_channel=0.5)
                         ])

for image in os.listdir(INPUT_IMG_DIR):
    image = image
    print(image)
    print(len(image))
    print(type(image))
    image = cv2.imread(image)
    
    seq_det = seq.to_deterministic()
    image_aug = seq.augment_images(image)
    print(image_aug)
    cv2.imwrite(OUTPUT_AUG_DIR, image, image_aug)

This code is not working for me. It is throwing error like this,

NORMAL_IMG_0.jpeg

<class 'str'>

None
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-28-b6ca6f4c6834> in <module>
      9     image_aug = seq.augment_images(image)
     10     print(image_aug)
---> 11     cv2.imwrite(OUTPUT_AUG_DIR, image, image_aug)

error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

Upvotes: 4

Views: 557

Answers (1)

lenik
lenik

Reputation: 23508

replace:

cv2.imwrite(OUTPUT_AUG_DIR, image, image_aug)

with:

cv2.imwrite(os.path.join( OUTPUT_AUG_DIR, image), image_aug)

Upvotes: 2

Related Questions