How to check dimensions of all images in a directory using python?

后端 未结 7 670
無奈伤痛
無奈伤痛 2020-12-09 05:15

I need to check the dimensions of images in a directory. Currently it has ~700 images. I just need to check the sizes, and if the size does not match a given dimension, it w

相关标签:
7条回答
  • 2020-12-09 06:09
    import os
    from PIL import Image 
    
    folder_images = "/tmp/photos"
    size_images = dict()
    
    for dirpath, _, filenames in os.walk(folder_images):
        for path_image in filenames:
            image = os.path.abspath(os.path.join(dirpath, path_image))
            with Image.open(image) as img:
                width, heigth = img.size
                SIZE_IMAGES[path_image] = {'width': width, 'heigth': heigth}
    
    print(size_images)
    

    In folder_images you arrow directory where it is imagens. size_images is a variable with the size of the images, in this format.

    Example
    {'image_name.jpg' : {'width': 100, 'heigth': 100} }
    
    0 讨论(0)
提交回复
热议问题