Connor's Blog

Fast Dimensions for Remote Images

I recently needed to get the dimensions of many thousands of images in a cloud blob storage container for a rethumbnailing tool. It would take a long time to download each image to check its dimensions. Instead, I implemented a basic algorithm using Pillow-SIMD and aiohttp, which grabs the minimum bytes necessary to discover the image dimensions. JPEG images do not contain the image dimensions in the header, and so they will require more iterations to discover their dimensions.

Most of the time taken using this algorithm is establishing the HTTP connection. If you have an array of URIs, you should call the get_dimensions coroutine for each and then call asyncio.wait to await them all at once, which will be significantly faster.

Here’s the code. I hope it can be useful to somebody.