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?
Advertisement
Answer
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.