Image Conversion (JPG ⇄ PNG/JPG ⇄ WEBP) with Python

Ajeet Verma
Dec 15, 2019 · 3 min read
Image for post
Image for post
test.png

Here we are going to use PIL(Python Imaging Library) or pillow library which is widely used for image processing in python and 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.

To load an image from a file, use the open() function in the Image module:

Image for post
Image for post

Image.convert() returns a converted copy of this image. For the “P” mode, this method translates pixels through the palette. If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette.

The current version supports all possible conversions between “L”, “RGB” and “CMYK.” The matrix argument only supports “L” and “RGB”.

save(fp, format) takes two input parameter, first file path(fp) to save the converted file and second the file format to convert into.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

References: PIL documentat, Image, Arrows

Hope this blog will be useful and feel free to ask questions and if you like it, please let me know in the comment section below. Happy :) reading!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store