dimanche 7 novembre 2021

iterating through a list of image objects to save file

I would like to randomly rearrange a file of images and save them in their new order in a new file. I have almost achieved my objective, but there is one problem. it will only save the last image in the list.

import os
from PIL import Image
import random
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)
img_list=[]
for f in os.listdir('.'):
    i=Image.open(f)
    img_list.append(i)
print(img_list)
random.shuffle(img_list)
filename = input('What is the file name \n')
makemydir(filename)
for img in img_list:
    
    print(img)
    img.show()
    img.save('{}.jpg'.format(f))
print(img_list)



Aucun commentaire:

Enregistrer un commentaire