Signup/Sign In

Convert PNG to JPG Image using Python

Posted in Programming   MAY 26, 2023

    JPG format is adopted by default for digital images in various devices for a long time now. Digital cameras store the images as JPG. When people take pictures for use on websites, they are already in JPG format. When people create image files whether it's full of text or is an actual picture, they'll save it in the format they're familiar with which is JPG or PNG.

    JPG TO PNG using Python


    As most of the software requires the image to be uploaded in JPG format, Here in this article, we will learn how to convert PNG image to JPG image by using Python script.
    To convert a PNG image to a JPG image, first we need an image, so choose any PNG file you need to convert to JPEG.

    Example: PNG To JPG using Python

    First, we require a PIL library (Python imaging library) to install in our system. To install this library, you have to type the command given below. Though you want to install in the Spyder or in PyCharm, the command remains the same.

    Fig : image of pip command in windows powershell

    Press the Run button. If you are installing it for the first time, then it will take a few seconds to install your particular. If the library already installed in your python interface, you will get the message as a requirement already satisfied.
    After proper installation of this command for the library to convert the image from PNG to JPG, we need to import the PIL library. The command is:

    From PIL import image
    image=image.open(‘path’) // image=image.open (‘c://temp/image').
    image.save((‘path’) // image.save (‘c://temp/image').

    After writing this command, we have to upload the image which you want to convert to JPG format.
    Next, provide the destination folder where you want to save that image.

    Here in the case, if you get an error such as RGBA error. RGBA means R for red, G for green B for blue and A for alpha. JPG does not support alpha JPG transparency. To remove this error and to troubleshoot this problem, we have to convert the image from RGBA to RGB format and then save the image. Here is the code for it, this command is similar to what we saw above with some changes.

    Image1=image.open(‘path’)
    Rgb=image1.convert(‘RGB’)
    Rgb.save(‘path’)

    First, we have to upload the image then the second thing we convert the RGBA image to the RGB image so for that we use a method that converts the image from RGBA to RGB.
    Once the image is converted, store the converted image in the variable called RGB then you can store the converted RGB format to the location where you want to store the image when you run this command, your PNG image is converted to a JPEG.

    Example: PNG To JPG using Python

    This is another example to convert PNG to JPG image in Python. First import the PIL file by using this statement.

    from PIL import image

    This is the complete code example to convert PNG image to JPG.

    import os
    import sys
    from PIL import Image
    #check whether or not the user passed the input image 
    if len(sys.argv) > 1:
        file =sys.argv[1]
        if os.path.exists(file):
            filename=file.split(".")
            img = Image.open(file)
            target_name = filename[0] + ".jpg"
            rgb_image = img.convert('RGB')
            rgb_image.save(target_name)
            print("Converted image saved as " + target_name)
        else:
            print(file + " not found in given location")
    else:
        print("please execute the script with input image as: python timageconvert.py <file>")

    Save the above script and give the name whatever you like, But the file name with which you are saving needed to be mentioned in the above script.

    As the last step, you execute the following command in the command prompt

    Python timageconvert.py <PNG image name>

    Now the image converts to JPG. You can execute the same script to convert any other format, such as (tiff) to JPG format.

    Conclusion:

    The JPG format was quite old, but it is the most preferable format even these days. The article gives you a brief idea of how to PNG to JPG using python and also to convert any other format to python.

    About the author:
    Tags:image-editorpngjpg
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS