Change language

Filtering images based on size attributes in Python

|

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

A simple Python3 function that enters a Python file path, threshold width in pixels, and threshold height in pixels, searches all images present in this single directory and creates a new directory by filtering all images according to a given threshold size, or resizing to a given width and height threshold.

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 implementation of the above approach in Python3:

# Python3 image filtering program
# based on size attributes

from PIL import Image

from shutil import copyfile

import os, os.path

 

 

def filterImages (path, thresholdWidth , thresholdHeight):

 

  # Define an array of images for

# identifying only image files

imgs = []

 

# List of possible image extensions

# add if you want more

valid_images = [ ". jpg" , ". gif " , ". png " , ". tga" ,

  ". jpeg " , ". PNG " , ". JPG " , ".jpeg" ]

 

# Store all images in an array of images ( imgs)

for f in os.listdir (path):

ext = os.path.split (ext (f) [ 1 ]

 

if ext.lower () not in valid_images:

continue

imgs.append (f)

 

  # Check if images are not filtered

# the directory exists or not

directory = os.path.dirname ( ’filteredImages’ + path)

  if not  os.path.exists (directory):

os.makedirs (directory)

 

# Defining FilterIMages array for

# store all the images we need

filteredImages = []

 

for i in imgs:

image = Image. open ( os.path.join ( (path, i)) 

 

# Preserve image width and height

  width, height = image.size

 

# if only the width exceeds thresholdWidth

if (width" thresholdWidth and

height " = thresholdHeight):

  

image.resize ((thresholdWidth, 

(thresholdWidth * height)

/ / width)). save (i)

 

# if only the height exceeds thresholdHeight

  elif (width " = thresholdWidth and

  height" thresholdHeight):

 

image.resize (((thresholdHeight * width)

/ / height, thresholdHeight )). save (i)

 

  # if both parameters exceed

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

 

# return a FilterImages array

return filteredImages

 
Driver code

if __ name__ = = ’__main__’ :

  

filteredImages = []

 

# Enter the python path sizeFilter

  # file, thresholdWidth (in pixels) and

# thresholdHeight (pixels)

filteredImages = filterImages ( "/ home / SahilKhosla / Desktop / Current Project" 1000 , 1000 )

Output:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method