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

后端 未结 7 672
無奈伤痛
無奈伤痛 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 05:47

    You can use the Python Imaging Library (aka PIL) to read the image headers and query the dimensions.

    One way to approach it would be to write yourself a function that takes a filename and returns the dimensions (using PIL). Then use the os.path.walk function to traverse all the files in the directory, applying this function. Collecting the results, you can build a dictionary of mappings filename -> dimensions, then use a list comprehension (see itertools) to filter out those that do not match the required size.

提交回复
热议问题