While we make websites where we need to upload images such as in event organizing server, the image for the event needs to be shown in various different sizes in different pages. But an image with high resolution might be an overkill for using at a place where we just need it to be shown as a thumbnail. So what most CMS websites do is re-size the image uploaded and store a smaller image as thumbnail. So how do we do that? Let's find out.
Here, I am going to discuss about how to do it in python. Python has a module named PIL (Python Image Library). We will be using PIL to resize image. Firstly, what we do is store the image in a temporary folder. Then we open the image as PIL Image object.
While re-sizing, we maintain the aspect ratio. Firstly, we fix on a particular width of the thumbnail. We get the height from the width fixed keeping the aspect ratio intact using the following code:
After we have the width and height that we want to resize the image to, we use the resize()
function of Image class in PIL. It takes a tuple (width, height) as a parameter.
After the image is resized, you can either save it in your local disk or upload it to your storage space.