Apply changes to all the images in given folder – Using Python PIL

Given a dataset of raw images, which usually need some pre-processing, which one person has to do physically. It is generally a task that requires some repetitive operation to perform for each image. Well, we can easily automate this process using some simple Python code and some libraries with it. So without further adieu, let’s see how to apply changes to all the images in given folder and save it to some destination folder using Python PIL.

Let’s install all the required modules –

pip3 install pillow
pip3 install os-sys

We will be parsing all the images in the folder to apply changes/operations to all of them simultaneously.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Code to apply operations on all the images
# present in a folder one by one
# operations such as rotating, cropping, 
from PIL import Image
from PIL import ImageFilter
import os
  
def main():
    # path of the folder containing the raw images
    inPath ="E:\\GeeksforGeeks\\images"
  
    # path of the folder that will contain the modified image
    outPath ="E:\\GeeksforGeeks\\images_rotated"
  
    for imagePath in os.listdir(inPath):
        # imagePath contains name of the image 
        inputPath = os.path.join(inPath, imagePath)
  
        # inputPath contains the full directory name
        img = Image.open(inputPath)
  
        fullOutPath = os.path.join(outPath, 'invert_'+imagePath)
        # fullOutPath contains the path of the output
        # image that needs to be generated
        img.rotate(90).save(fullOutPath)
  
        print(fullOutPath)
  
# Driver Function
if __name__ == '__main__':
    main()

chevron_right


Sample images from the folder –

Input :

image_sample1



Output :

invert_image_sample1




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.