So I am trying to use python's argparse to get images from a directory modify them and store them in a new directory but I'm having trouble.
For example say I have a directory named classes which has 50 different classes each having 100 different images.
classes > {A(50), B(50), ...N(50)}
Then I should so something with these images and save them into a new different directory classes_2
classes2 > {A'(200), B'(200),...N'(200)}
I want to be able to specify something
python filename.py --input classes/A --dest classes_2/A'
How can I use argparse in order to do this. Also I'm using scipy imsave however I have to change the path in my code every single time.
Edited:
Ok thank you @nosklo, Now the problem that I'm having is this I'm using imgaug library for image augmentation and my code is this:
suppose I have all the imports needed, cv2, imgaug, argparse, os, sys, etc.
parser = argparse.ArgumentParser()
parser.add_argument('--input', help = 'input directory', required = True)
parser.add_argument('--dest', help = 'destination directory', required = True)
args = ap.parse_args()
for filename in files:
images = []
image = cv.imread(filename)
image_rgb = cv.cvtColor(image, cv.COLOR_BRG2RGB)
images.append(image_rgb)
#imgaug built-in functions
seq = iaa.Sequential([ ... ])
num_augs_per_image = 10
# save images in format 001_07
# where 001 is the second image in your original data set
# and 07 is the 8th version of the 2nd image
for i, image in enumerate(images):
image_augs = seq.augment_images([image] * n_augs_per_image)
for j, image_aug in enumerate(image_augs):
result = os.path.join(args.dest, "%03d_%02d.jpg" % (i, j))
with open(result, 'wb') as f:
f.write(image_aug)
I've also tried for the last part:
for i, image in enumerate(images):
image_augs = seq.augment_images([image] * n_augs_per_image)
for j, image_aug in enumerate(image_augs):
misc.imsave(os.path.join("%03d_%02d.jpg" % (i, j)), image_aug)
However when I call the file python filename.py --input /path/classes/A --dest /path/classes/BI do not get any of images saved in the new directory.
Like for example if I had 5 images in directory AI was expecting to have 50 images in directory B since I have augmented each image 10 times and want to save all versions of my original images.
I will help with the argparse part, so you can focus on your "something" you must do to the images:
import os
import argparse
parser = argparse.ArgumentParser(description='Process some images')
parser.add_argument('--input', help='the input directory', required=True)
parser.add_argument('--dest', help='the destination', required=True)
args = parser.parse_args()
for path, dirs, files in os.walk(args.input):
for filename in files:
fullname = os.path.join(path, filename)
result, result_filename = do_something(fullname)
result_fullname = os.path.join(args.dest, result_filename)
with open(result_fullname, 'wb') as f:
f.write(result)
This assumes your function do_something
will be able to return a result
and a new result_filename
that will be used to write the file in the --dest
directory - if you want to use the same filename just use the filename
variable instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.