Alex Rivera | Logout

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

Asked 2009-10-01T23:47:48.333
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 will be moved to a different folder. How do I get started?

Edit
Report

1 Answer

8

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.

answered 2009-10-01T23:55:11.733

Your Answer