Saturday, November 16, 2024
Google search engine
HomeLanguagesFiltering Images based on size attributes in Python

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 :




# 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 parameters 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)


 
Output :

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments