here is a simple code to convert dir/file of images to png in batch.

import os, glob, argparse, datetime, subprocess


parser = argparse.ArgumentParser()

parser.add_argument('inputs', type=str, help='path to input')


args = parser.parse_args()


input_path = args.inputs

assert os.path.exists(input_path)

if os.path.isdir(input_path):
    input_files = glob.glob(os.path.join(input_path, '*'))
else:
    input_files = [input_path]
    

timestamp=datetime.datetime.now().strftime("%y%m%d_%H%M%S")

outputdir = 'testoutput/{}'.format(timestamp)
os.makedirs(outputdir)
print("outputdir: {}".format(outputdir))


for f in input_files:
    basename = os.path.basename(f)
    filename, _ = os.path.splitext(basename)
    
    savepath = os.path.join(outputdir, '{}.png'.format(filename))
    
    cmd = 'convert {} {}'.format(f, savepath)
    subprocess.run(cmd, shell=True)
   

for installing imagemagick, use this command:

$ sudo apt install imagemagick

0 Comments

Leave a Reply

Your email address will not be published.