Apply changes to all the images in given folder - Using Python PIL
Last Updated : 21 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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.
Python3 1==
# Code to apply operations on all the images# present in a folder one by one# operations such as rotating, cropping, fromPILimportImagefromPILimportImageFilterimportosdefmain():# path of the folder containing the raw imagesinPath="E:\\GeeksforGeeks\\images"# path of the folder that will contain the modified imageoutPath="E:\\GeeksforGeeks\\images_rotated"forimagePathinos.listdir(inPath):# imagePath contains name of the image inputPath=os.path.join(inPath,imagePath)# inputPath contains the full directory nameimg=Image.open(inputPath)fullOutPath=os.path.join(outPath,'invert_'+imagePath)# fullOutPath contains the path of the output# image that needs to be generatedimg.rotate(90).save(fullOutPath)print(fullOutPath)# Driver Functionif__name__=='__main__':main()
Sample images from the folder -
Input :image_sample1Output :invert_image_sample1
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.