How to Convert Image File Format Using Python

Python is known for its versatility. you may create real-world utility tools in Python that can simplify and automate certain tasks.

Learn how to build an image type converter with just a few simple lines of Python code. Whether it’s a single image file or all files in a directory, you can easily convert between different formats.

4

Installing Required Libraries

You need to install thePillowPython library to build an image-type converter in Python. This library advances the image-processing capabilities of your Python interpreter. You can create a general image processing tool using several modules of this library. Some of the most useful are theImage,ImageFile,ImageFilter, andImageStatmodules.

Run the following command in the terminal to install the Pillow Python library:

Image format converter using Python

Once you have Pillow installed on your system, you are ready to work with images.

Loading and Displaying Properties of an Image

First you need to import theImagemodule from thePILlibrary to set up the code. Next, you need to use theImage.open()method to load the image and assign it to a variable. Once you have loaded the image, you may display it using theshow()method.

The Image format converter code is available in aGitHub repositoryand is free for you to use under the MIT License.

Python output showing image metadata from a file

The image that you passed as a parameter to theopen()method will open up after you execute the code. This is a good first step, as a sanity check, to ensure you have successfully installed the library on your system.

The Image module provides several other properties that you could use to get more information about the image.

A view from above of hands typing on a laptop with green programming code shown on-screen

You should see some meaningful data with no errors:

How to Convert Image Format Using Python

You can simply convert the file format of an image using thesave()method. You just need to pass the newfilenameand extension as a parameter to thesave()method. Thesave()method will automatically identify the extension that you passed and then save the image in the identified format. But before using thesave()method, you may need to specify the mode of the image (RGB, RGBA, CMYK, HSV, etc.).

According tothe official pillow documentation, the mode of an image is a string that defines the type and depth of a pixel in the image. The pillow library supports 11 modes including the following standard modes:

windows pc manager with ram boost option.

RGB(3x8-bit pixels, true color)

RGBA(4x8-bit pixels, true color with transparency mask)

CMYK(4x8-bit pixels, color separation)

HSV(3x8-bit pixels, Hue, Saturation, Value color space)

How to Convert an Image From PNG to JPG and JPG to PNG

You need to pass the stringfilename.jpgas a parameter to thesave()method to convert image files in anyformat (PNG, GIF, BMP, TIFF, etc.) to JPG format. Also, you need to provide the mode of the image. The following code converts an image fromPNG format to JPG format:

You’ll lose any transparency in an image if you convert it to JPG format. If you strive to preserve the transparency using theRGBAmode, Python will throw an error.

You can convert an image in any format to PNG format using thesave()method. You just need to pass the PNG image as a parameter to thesave()method. The following code converts an image from JPG format to PNG format:

Converting an image to PNG preserves any transparency. For example, if you convert a transparent GIF image to a PNG image, the result will still be a transparent image.

How to Convert an Image to Any Other Format Using Python

Similar to the steps above, you can convert an image in any format to any other format using thesave()method. You just need to provide the correct image extension (.webp, .png, .bmp, etc.) to thesave()method. For example, the following code converts an image from PNG to WebP format:

Error Handling for Missing Image Files

In case the code is not able to find the input image, it will throw an error. You can handle this using theFileNotFoundErrorPython exception.

Converting All the Images in a Directory to a Different Format

If there are several image files in a directory, that you want to convert to a different format, you can easily do so with just a few lines of code in Python. You need to import thegloblibrary to iterate through the files in the current directory or inside a given folder. The following code converts all the JPG images in the current directory to PNG format:

If you want to convert a different set of files, change the string parameter you pass to theglob()method.

Build a GUI Using Python

Python libraries like Pillow make it easy to develop tools to deal with images in Python. you could perform tasks quickly with a command line interface but a GUI is essential to create a user-friendly experience. You can create more specialized GUI applications using Python frameworks like Tkinter and wxPython.

These frameworks help make easy work of Python GUI development.

Freeing up vital memory on Windows only takes a moment, and your computer will feel much faster once you’re done.

Your phone is a better editor than you give it credit for.

These plugins will make you wonder why you used Photoshop in the first place.

Quality apps that don’t cost anything.

When your rival has to bail out your assistant.

Technology Explained

PC & Mobile