Reputation: 11
I'm trying to convert multiple JPG files into PNG files. I'm able to do it for a single file but the loop doesn't seem to work for multiple files. Could you please help with that? I'm sharing my code below:
from PIL import Image
img = Image.open('./image.jpg')
img.save('new_image.png','png')
print('All done!')
Upvotes: 0
Views: 2496
Reputation: 392
You can try this -
from PIL import Image
import glob
counter = 0
for image in glob.glob("./*.jpg"):
counter = counter + 1
img = Image.open(image)
img.save(str(counter)+'new_image.png','png')
Upvotes: 3
Reputation: 11
#So the following code worked. Sorry for the formatting, I'm just beginning to learn!
from PIL import Image import glob import os
directory = r'C:\Users\Umar Iqbal\Desktop\newfolder' #this is where we will save our converted images
for image in glob.glob('./*.jpg'): img = Image.open(image)
clean_name = os.path.splitext(image)[0] #if we don't use this, we get jpg in the file's name
img.save(f'{directory}{clean_name}.png', 'png') #this allows us to save the new images in the directory
Upvotes: 0