📜  在Python中根据大小属性过滤图像

📅  最后修改于: 2022-05-13 01:55:05.685000             🧑  作者: Mango

在Python中根据大小属性过滤图像

先决条件:PIL_working-images-python

给定一个图像目录,我们的程序将根据给定的阈值大小创建一个新的图像目录。

一个简单的 Python3函数,输入Python文件的路径,以像素为单位的阈值宽度和以像素为单位的阈值高度,搜索该目录中存在的所有图像并创建一个新目录,根据给定的阈值大小过滤掉所有图像,或将其调整为给定的阈值宽度和高度。


以下是上述方法的 Python3 实现:

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


输出 :