How to save images in a folder with for loop?

Maria Lavrovskaya picture Maria Lavrovskaya · May 26, 2019 · Viewed 10.5k times · Source

First, I have an issue with saving up every resized file with the same name to the same folder? Second, while running I can't understand if m t code works properly. Please, could you check if I am doing the resizing properly?Can't find a mistake in my code:

import glob
from PIL import Image
images = glob.glob("C:/Users/marialavrovskaa/Desktop/Images/*.png")
for image in images:
    with open(image,"rb") as file:
        img = Image.open(file)
        imgResult = img.resize((800,800), resample = Image.BILINEAR)
      imgResult.save('"C:/Users/marialavrovskaa/Desktop/Images/file_%d.jpg"', 'JPEG')
        print("All good")

Answer

Rabbid76 picture Rabbid76 · May 26, 2019

If you want to give the images a name with a consecutive number than you've to concatenate the file name and a counter:

image_no = 1
for image in images:

    # [...]

    name = 'C:/Users/marialavrovskaa/Desktop/Images/file_' + str(image_no) + '.jpg'
    imgResult.save(name, 'JPEG')
    image_no += 1

Since the format of the images is PNG and they should be stored as JPEG, the format has to be convert from RGBA to RGB, by .convert('RGB'). Note, storing a RGBA image to 'JPGE' would cause an error:

import glob
from PIL import Image
images = glob.glob("C:/Users/marialavrovskaa/Desktop/Images/*.png")
image_no = 1
for image in images:
    with open(image,"rb") as file:
        img = Image.open(file)
        imgResult = img.resize((800,800), resample = Image.BILINEAR).convert('RGB')
        name = 'C:/Users/marialavrovskaa/Desktop/Images/file_' + str(image_no) + '.jpg'
        imgResult.save(name, 'JPEG')
        image_no += 1
        print("All good")

By the way, if the file name should by kept and the image just should be stored to a file with a different extension, then then extension can be split form the file by .splitext:

import os
imgResult = img.resize((800,800), resample = Image.BILINEAR).convert('RGB')
name = os.path.splitext(image)[0] + '.jpg'
imgResult.save(name, 'JPEG')

If you wan to store the fiel to a different path, with a different extension, then you've to extract the fiel name from the path.

See os.path. Split the path from the file name and extension by os.path.split(path), which returns a tuple of path and name.

e.g.

>>> import os
>>> os.path.split('c:/mydir/myfile.ext')
('c:/mydir', 'myfile.ext')

The split the file name and the extension by os.path.splitext(path):

>>> os.path.splitext('myfile.ext')
('myfile', '.ext')

Applied to your code this means, where file is the path, name and extension of the source image file:

import glob
from PIL import Image
images = glob.glob("C:/Users/marialavrovskaa/Desktop/Images/*.png")
image_no = 1
for image in images:
    with open(image,"rb") as file:
        img = Image.open(file)
        imgResult = img.resize((800,800), resample = Image.BILINEAR).convert('RGB')

        image_path_and_name = os.path.split(file) 
        image_name_and_ext = os.path.splitext(image_path_and_name[1]) 
        name = image_name_and_ext[0] + '.png'
        file_path = os.path.join(path, name)

        imgResult.save(file_path , 'JPEG')
        image_no += 1
        print("All good")