You are currently viewing Resizing Uploaded Image (Python)

Resizing Uploaded Image (Python)

While we make websites were 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:
Screenshot from 2016-08-14 04:23:05

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.
Screenshot from 2016-08-14 04:28:13

After the image is resized, you can either save it in your local disk or upload it to your storage space.

saptaks

Full Stack Developer, Open Source enthusiast

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.