Codementor Events

Python Image Library: Convert Type and Quality of Image

Published Jun 02, 2017
Python Image Library: Convert Type and Quality of Image

jpeg png difference

Image upload is an important part of the server. The images can come in certain formats and be transformed into other formats after applying certain JavaScript modifications. For example, when an image is uploaded after being cropped in Open Event Organizer Server, it is automatically saved as a PNG file. However, PNG format is more than five times larger than JPEG image format. So when we upload a 150KB image, the image will reach the server at around 1MB, which is huge. Therefore, we need to decide in the server which image format to select in different cases, and how to convert them.

JPEG – JPEG files 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 much smaller compared to that of PNG. However, JPEG files cannot store the alpha transparency information of the image. So, if transparency information isn’t required, JPEG 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 also supports alpha transparency. A photograph saved as a PNG will likely be at least 5 times larger than an equivalent JPEG image, offering a little improvement in visible quality. So, if file size isn’t a problem and you want the best possible quality, then PNG is the way to go.

In our case, most images will be uploaded to our website. So, the smaller the better. We therefore store most large image files in JPEG format by converting PNG to JPEG using PIL (Python Image Library).

PNG to JPEG Conversion

The Python Imaging Library, or PIL in 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 JPEG, we create a new image file in JPEG format, read the PNG image, then paste the read image to the newly created blank JPEG image. Then the JPEG image file is saved. This is the basic idea behind conversion from PNG to JPEG.

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

As you can see, when saving a JPEG image, we can specify the quality it is to be saved in. So if we want, we can take the quality input for conversion from the admin, and then decide the quality while changing from PNG to JPEG.

This is an important feature because most large images, such as backgrounds or header backgrounds, are already of a huge size. Manipulating such images and saving the files in PNG format only increases the size further.. So, considering ease of page design, you may find it more convenient to store such images in JPEG format.This will make your website more scalable, and make image upload faster.


This post was originally posted by the author on fossasia. This version has been edited for clarity and may appear different from the original post.

Discover and read more posts from Saptak Sengupta
get started
post comments1Reply
Andrey Gontchar
5 years ago

According to PIL documentation, there’s no need to create a new JPG file.

You can just save the im image directly to JPG

im.save('file.jpg', quality=95)