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:
Code language: Python (python)
import os import sys from PIL import Image if len(sys.argv) > 1: if os.path.exists(sys.argv): im = Image.open(sys.argv) target_name = sys.argv + ".jpg" rgb_im = im.convert('RGB') rgb_im.save(target_name) print("Saved as " + target_name) else: print(sys.argv + " not found") else: print("Usage: convert2jpg.py <file>")
This is a python script that converts photos to jpg format If you want to run it the command is :
$ convert2jpg test.png
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!