How to Convert PNG to JPG using Python

If we want to convert a image files format, we have plenty of online tools to convert, we can use those tools for unofficial purpose, but we cannot use that for personal use because we are uploading our image file to unknown web servers to do the process. To solve this we can write our own script to convert file format without the help of a server.

In this article we are going to convert image format from PNG to JPG using Python programming language. Follow the below steps to convert the image format using python.

Steps to Convert PNG to JPG using Python:

To convert a PNG image to JPG using Python, you can use the Pillow library. Here is an example of how you can do this:

Step 1: Install PIL Python Package

To install PIL python package, open your Command Prompt by searching “cmd”, now paste the below command in your command prompt and press enter. This will install PIL package for you to convert PNG image to JPG.

pip install Pillow

Step 2: Go to the file path where PNG is stored

Once PIL package is installed, now we want to write the python script to convert the image. To do that 1st we need to navigate to the folder where .png is located. Now create a notepad file inside that folder and paste the below code in the new notepad file that we created,

from PIL import Image

# Open the PNG image
with Image.open('image.png') as img:
    # Convert the image to JPG
    img = img.convert('RGB')
    # Save the image with the .jpg extension
    img.save('image.jpg')

save the file with .py as extension(Eg: conv_png_jpg.py).

Note: Make sure to rename image.png with your image file name.

Step 3: Execute python code

To execute the python code we need to need to open command prompt and navigate to the folder where we saved the python script and .png image. Use cd command to navigate(Eg: >cd folder_name). To run the python script use the below command,

python conv_png_jpg.py

Note: replace the conv_png_jpg.py file name with yours.

Once the python script is executed it will generate .jpg image file in the same folder.

Alternatively, you can use the Python imaging library (PIL) to achieve the same result. Here is an example using PIL:

from PIL import Image

# Open the PNG image
img = Image.open('image.png')
# Convert the image to JPG
img = img.convert('RGB')
# Save the image with the .jpg extension
img.save('image.jpg')

Learn about PNG and JPEG:

About PNG:

PNG (Portable Network Graphics) is a file format for storing raster graphics images. It was designed to replace the GIF format, which was patented by CompuServe. Unlike the GIF format, which uses lossless compression, PNG uses lossless data compression, which makes it well-suited for storing images with large areas of solid color or with sharp transitions between colors.

PNG supports a range of bit depths, from 1-bit (black and white) to 32-bit (full color with alpha channel). It also supports transparency, which allows an image to have transparent areas. This makes it a popular format for use on the web, where it is often used to display images with transparent backgrounds.

PNG is a popular choice for storing images because it is lossless, supports transparency, and has good support in web browsers. However, it is not as efficient as the JPEG format, which uses lossy compression and is better suited for storing photographic images.

About JPEG:

JPEG (Joint Photographic Experts Group) is a file format for storing digital images. It was designed to be a lossy format, which means that it uses a form of data compression that reduces the size of the file by discarding some of the data. This makes it well-suited for storing photographic images, which often contain a lot of detail and can be quite large in size.

JPEG uses a type of data compression called discrete cosine transform (DCT), which is based on the way the human eye perceives images. It works by dividing the image into blocks of pixels, and then applying a mathematical transformation to each block to compress the data. The compressed data is then encoded using a method called Huffman coding, which further reduces the size of the file.

JPEG is a popular format for storing digital photographs because it is efficient and produces good-quality images. However, because it is a lossy format, it is not well-suited for storing images that need to be edited or images that contain text, line art, or other graphics with sharp transitions between colors. In these cases, a lossless format such as PNG is a better choice.

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *