Filtering Images based on size attributes in Python

Prerequisite : PIL_working-images-python

Given an Image directory, our program will create a new image directory based on given threshold size.

A simple Python3 function that inputs the path of python file, the threshold width in pixels and the threshold height in pixels, searches all the images present in that only directory and creates a new directory having filtered out all the images according the given threshold size, or resizes it to the given threshold width and height.

Steps to Follow :

1. Install necessary Libraries like PIL
2. Import libraries : PIL, shutil, os, os.path
3. Save the code as sizeFilter.py
4. Open Terminal (where the python file is present and run)-> python sizeFilter.py

 
Below is the Python3 implementation of above approach :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Filtering Images
# based on Size Attributes 
from PIL import Image
from shutil import copyfile
import os, os.path
  
  
def filterImages(path, thresholdWidth, thresholdHeight):
      
    # Defining images array for
    # identifying only image files
    imgs = []
      
    # List of possible images extensions
    # add if you want more
    valid_images = [".jpg", ".gif", ".png", ".tga",
                    ".jpeg", ".PNG", ".JPG", ".JPEG"]
  
    # Storing all images in images array (imgs)
    for f in os.listdir(path):
        ext = os.path.splitext(f)[1]
          
        if ext.lower() not in valid_images:
            continue
        imgs.append(f)
  
    # Checking whether the filteredImages
    # directory exists or not
    directory = os.path.dirname('filteredImages' + path)
    if not os.path.exists(directory):
        os.makedirs(directory)
  
    # Defining filteredIMages array for
    # storing all the images we need
    filteredImages = []
      
    for i in imgs:
        image = Image.open(os.path.join(path, i))
          
        # Storing width and height of a image
        width, height = image.size
  
        # if only width exceeds the thresholdWidth
        if (width > thresholdWidth and
            height <= thresholdHeight):
                  
            image.resize((thresholdWidth, 
                        (thresholdWidth * height)
                                // width)).save(i)
  
        # if only height exceeds the thresholdHeight
        elif (width <= thresholdWidth and
              height > thresholdHeight):
                    
            image.resize(((thresholdHeight * width)
                        // height, thresholdHeight)).save(i)
  
        # if both the paramateres exceeds
        # the threshold attributes
        elif (width > thresholdWidth and
              height > thresholdHeight):
                    
            image.resize((thresholdWidth, thresholdHeight)).save(i)
  
        copyfile(os.path.join(path, i),
                 os.path.join(path + '/filteredImages', i))
          
        filteredImages.append(i)
          
    # returning the filteredImages array
    return filteredImages
  
# Driver Code
if __name__ == '__main__':
      
    filteredImages = []
       
    # Enter the path of the python sizeFilter
    # file, the thresholdWidth(in pixels) and
    # thresholdHeight(in pixels)
    filteredImages = filterImages("/home/SahilKhosla/Desktop/Current Project", 1000, 1000)

chevron_right


 
Output :



My Personal Notes arrow_drop_up

Always code as if the guy who ends up maintaining your code will be violent psychopath who knows where you live visit wwwsahilkhoslacoin

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.