How To Convert PNG To JPG in Python?


Python is a versatile language that is widely used in many different industries today. One common use case for Python is image processing. In this blog post, we will show you how to convert a PNG file to a JPG file using Python.

There are many reasons why you would want to convert a PNG file to a JPG file. For example, JPG files are generally smaller in size than PNG files, so converting PNG to JPG can save you some disk space. Additionally, some applications only support JPG format images and not PNG format images.

How To Convert PNG To JPG in Python?

Converting PNG to JPG in Python is very easy thanks to the Pillow library. Pillow is a popular image processing library that is available for free. To install Pillow, use the following command:

pip install pillow

Once you have installed Pillow, you can now start writing the code to convert your PNG file to a JPG file. The code is as follows:

import os import sys from PIL import Image if len(sys.argv) > 1: if os.path.exists(sys.argv[1]): im = Image.open(sys.argv[1]) target_name = sys.argv[1] + ".jpg" rgb_im = im.convert('RGB') rgb_im.save(target_name) print("Saved as " + target_name) else: print(sys.argv[1] + " not found") else: print("Usage: convert2jpg.py <file>")
Code language: Python (python)

This is a python script that converts photos to jpg format If you want to run it the command is :

$ convert2jpg test.png

Download Script Here

If you need to convert a PNG file into a JPG file for any reason, then this blog post has shown you how easy it is to do so using Python and Pillow—a popular image processing library. All you need is just a few lines of code and you’re good to go!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts