import os import PIL.Image def save_image(arr, fp, format='JPEG'): image = PIL.Image.fromarray(arr) image.save(fp, format=format, quality=90) def save_images(ims, output_dir='', prefix='', format='JPEG'): for i, im in enumerate(ims): full_path = os.path.join(output_dir, prefix + str(i).zfill(4) + '.' + format.lower()) save_image(im, full_path, format)