Convert JPG to PNG using Python

To convert JPG to PNG using Python:

from PIL import Image

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

Steps to Convert JPG to PNG using Python

Step 1: Install the PIL Package

To start, install the PIL package using the command below:

pip install Pillow

Step 2: Capture the Path where the JPG is Stored

Next, capture the path where your JPG file is stored.

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

C:\Users\Ron\Desktop\Test\autumn.jpg

Step 3: Convert the JPG to PNG using Python

Finally, use the template below in order to convert the JPG to PNG using Python:

from PIL import Image

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

For our example:

from PIL import Image

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

Run the code (adjusted to your paths), and the new PNG file will be created at your specified location.

Alternatively, check the following guide for the steps to convert PNG to JPG using Python.

Leave a Comment