• Register

How to Use Python to Convert PNG to JPG

 

To convert PNG to JPG using Python, you can use the following technique:

from PIL import Image

 

im1 = Image.open(r'path where PNG\file name.png 'is stored)

Im1.save(r'path where the JPG file name will be stored\new.jpg)

Next, using a simple example, I will analyse the steps for converting PNG to JPG. I'm also going to provide the code to build the following file converter tool:

<!--[if gte vml 1]> <![endif]-->How to Convert PNG to JPG using Python

Steps to Convert PNG to JPG using Python

Step 1: Install the PIL package

You may use the PIL kit to perform the conversion from PNG to JPG.

Therefore, you can instal this package by using the following command (under Windows):

pip install Pillow

You may want to refer to the steps for installing a Python package under Windows in the following guide.

Step 2: Capture the route by which the PNG is stored

Next, catch the path where your machine stores the PNG file.

 

In my case, under the following direction, I saved a PNG file (called 'summer'): C:\Users\Ron\Desktop\Test

<!--[if gte vml 1]> <![endif]-->Example of path

Step 3: Using Python to transform PNG to JPG

Finally, to convert your PNG to JPG, you can use the template below to:

from PIL import Image

 

Im1 = Image.open(r'path where PNG\file name.png 'is stored)

Im1.save(r'path where the JPG file name will be stored\new.jpg)

So with our example:

The path to where the PNG is currently stored is as follows: C:\Users\Ron\Desktop\Test

Where the file name is 'summer' and the extension of the file is 'png'

The path to where the JPG is stored is as follows: C:\Users\Ron\Desktop\Test\Test

"o Where the current name of a file is" new season "and the file extension is" jpgg

For example, this is the complete Python code to convert PNG to JPG (make sure to change the paths to represent the location where the files will be stored on your computer):

from PIL import Image

 

im1 = Image.open(r'C:\Users\Ron\Desktop\Test\summer.png')

im1.save(r'C:\Users\Ron\Desktop\Test\new_summer.jpg')

Run the code (after changing the paths) and the new JPG file will be displayed at your specified location.:

<!--[if gte vml 1]> <![endif]-->Convert PNG to JPG using Python

Tool to Convert PNG to JPG

To convert your PNG to JPG, you can use the tool below. The tool is based on the tkinter kit, which can be used to build a Python Graphical User Interface..

Canvas1 = tk.Canvas(root, width= 300, height= 250, bg= 'azure3', relief= 'raised')

Pack) (canvas1.pack)

 

Label1 = tk.Label(root, text='File Conversion Tool ', bg =' azure3 ')

Font=('helvetica, '20)) label1.config

Window canvas1.create(150, 60, window = label1)

 

(): def getPNG):

Global im1 worldwide

 

File path import = filedialog.askopenfilename)

Im1 = Image.open(path file import)

 

BrowseButton PNG = tk.Button(text='Import PNG file ', command=getPNG, bg='royalblue', fg='white ', font=('helvetica', 12,'bold '))

Window(150, 130, window = browseButton PNG) canvas1.create

 

ConvertToJPG) (def:

Global im1 worldwide

 

File path export = filedialog.asksaveasfilename(defaultextension='.jpg)

Im1.save(file path export)

 

SaveAsButton JPG= tk.Button(text='Convert PNG to JPG ', command=convertToJPG, bg='royalblue', fg='white ', font=('helvetica', 12,'bold '))

Window(150, 180, window = saveAsButton JPG) canvas1.create

 

About root.mainloop)

Run the code in Python, and you’ll see the following display:

<!--[if gte vml 1]> <![endif]-->How to Convert PNG to JPG using Python

Click on the ‘Import PNG File‘ button:

<!--[if gte vml 1]> <![endif]-->Tool to import file

Locate the PNG file you want to convert, then press Open:

<!--[if gte vml 1]> <![endif]-->Open a file

First, to do the conversion, press the 'Convert PNG to JPG' button:

<!--[if gte vml 1]> <![endif]-->How to Convert PNG to JPG using Python

Type a name such as 'new summer' for your new JPG file, and then press Save:

<!--[if gte vml 1]> <![endif]-->Save new file

At your specified venue, your new JPG file will appear:

<!--[if gte vml 1]> <![endif]-->Convert PNG to JPG using Python

Often, JPG will need to be converted to PNG. If that's the case, you might want to check the following guide to clarify the steps for using Python to convert JPG to PNG.

100 points
2