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

后端 未结 7 721
無奈伤痛
無奈伤痛 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:00

    One common way is to use PIL, the python imaging library to get the dimensions:

    from PIL import Image
    import os.path
    
    filename = os.path.join('path', 'to', 'image', 'file')
    img = Image.open(filename)
    print img.size
    

    Then you need to loop over the files in your directory, check the dimensions against your required dimensions, and move those files that do not match.

提交回复
热议问题