PIL to convert type and quality of image

Image upload is an important part of the server. The images can be in different formats and after applying certain javascript modifications, they can be changed to different formats. For example, when an image is uploaded after cropping in open event organizer server, it is saved in PNG format. But PNG is more than 5 times larger than JPEG image. So when we upload a 150KB image, the image finally reaching the server is around 1MB which is huge. So we need to decide in the server which image format to select in different cases and how to convert them.

JPEG – JPEGs images were designed to make detailed photographic images as small as possible by removing information that the human eye won’t notice. So the size of the image is pretty less compared to that of PNG. However in JPG image, you cannot store alpha transparency information of the image. So, if transparency information isn’t required, JPG is the most scalable solution.

PNG – PNG is a great format that combines Lossless encoding with Direct color (thousands of colours, just like JPEG). It supports alpha transparency. A photograph saved as a PNG will likely be at least 5 times larger than an equivalent JPEG image, with very little improvement in visible quality. So, if file size isn’t a problem and you want the best possible quality then PNG is good.

In our case, most images are used to be shown in website. So, smaller the size, better. So we store most of the big images in JPG format by converting PNG to JPG using PIL (Python Image Library)

PNG to JPEG Conversion

The Python Imaging Library, or PIL for short, is one of the core libraries for image manipulation in Python. Unfortunately, its development has stagnated, with its last release in 2009.

Luckily for you, there’s an actively-developed fork of PIL called Pillow – it’s easier to install, runs on all operating systems, and supports Python 3.

The most important class in the Python Imaging Library is the Image class, defined in the module with the same name. You can create instances of this class in several ways; either by loading images from files, processing other images, or creating images from scratch.

While converting from PNG to JPG, we create a new image file in jpeg format, read the png image, then paste the image read to the newly created blank jpg image. Then the jpg image file is saved. This is the basic idea behind conversion from PNG to JPG.

Screenshot from 2016-08-25 13:37:23

As we can see, while saving a jpg image we can mention the quality that we want it to be saved in. So if we want, we can take the quality input for conversion from the admin and then accordingly decide the quality while changing from PNG to JPG.

This is an important feature because most large images such as background, header background, etc. are already of a huge size and operations on them and saving in PNG upscales the size a lot. So, it is always better to store such images in JPG format to make your website more scalable so that images can be loaded faster.

saptaks

Full Stack Developer, Open Source enthusiast

Leave a Reply

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