Convert PNG to JPG using Python

To convert PNG to JPG using Python:

from PIL import Image

im = Image.open(r"path where the PNG is stored\file name.png")
im.save(r"path where the JPG will be stored\new file name.jpg")

Steps to Convert PNG to JPG using Python

Step 1: Install the PIL package

You can install the PIL package using the command below:

pip install Pillow

Step 2: Capture the path where the PNG is stored

Next, capture the path where the PNG file is stored on your computer.

For example, let’s suppose that a PNG file (called ‘summer‘) is stored under the following path:

C:\Users\Ron\Desktop\Test\summer.png

Step 3: Convert the PNG to JPG using Python

Finally, you may use the template below in order to convert your PNG to JPG:

from PIL import Image

im = Image.open(r"path where the PNG is stored\file name.png")
im.save(r"path where the JPG will be stored\new file name.jpg")

For our example:

from PIL import Image

im = Image.open(r"C:\Users\Ron\Desktop\Test\summer.png")
im.save(r"C:\Users\Ron\Desktop\Test\new_summer.jpg")

Run the code (adjusted to your paths), and you’ll get the new JPG file at your specified location.

Alternatively, you may wish to check the following guide that explains how to convert JPG to PNG using Python.

Leave a Comment