In this post, we will learn how to convert JPG to PNG in Python with detailed explanations and examples. So let’s get started.

You can also checkout this post πŸ‘‰ Remove Background From Images in Python

To convert JPG to PNG, first, we need to install the Python Pillow library. Pillow is an external Python library that allows us to change the extension of our images. To install it, run the command below in your terminal.

pip install pillow

After Installation is done, you need to follow the following steps to convert the JPG image to PNG.

Steps to Convert JPG to PNG

  1. Import image module from pillow library.
    • from PIL import Image.
  2. Open your JPG image using the open() function from the Image module of the Pillow library and save it in variable called img.
    • img =
  3. After opening your JPG image, use the save() method to save it with a PNG extension.

After completing the above three steps your JPG image has been successfully converted to PNG.

Source Code

from PIL import Image

img ="dog.jpg")"dog1.png")
print("Successfully Converted (JPG --> PNG)")


Successfully Converted (JPG --> PNG)

NOTE: Both my source code file and the image are located in the same directory, so I only need to write the name of the file. If your image file is located in any other place, you have to mention the absolute path of your image.

Similarly, you can change your JPG image to WEBP image. Here is the Source Code.

Convert JPG to WEBP

from PIL import Image

img ="dog.jpg")"dog.webp")
print("Successfully Converted (JPG --> WEBP)")


Successfully Converted (JPG --> WEBP)

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment