How to resize an image using PIL and maintain its aspect ratio?

Posted by Ramesh Singh | Updated on

Want to resize an image using PIL and maintain its aspect ratio? Define a maximum size. Then, compute a resize ratio by taking min(maxwidth/width, maxheight/height). The proper size is oldsize*ratio.

There is of course also a library method to do this: the method Image.thumbnail. Below is an (edited) example from the PIL documentation:


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers