Coders Packet

PNG to WEBP Converter in Python

By Amarjeet Singh Chauhan

Created a project for converting PNG images to WEBP format using python and its libraries. Taking png image and resulting webp (empty) file as inputs.

INPUT IMAGE: -

OUTPUT WEBP FORMAT: - (Link)

https://drive.google.com/file/d/1iAsy2MwcO8waEaSTbZ_NADYGbYaUyzqR/view?usp=sharing

 

PRE-PREP:

1.) .png image

2.) .webp output file

 

METHODOLOGY:

convert_png_to_webp

This function takes png image and output webp file as input and converts the png to webp format and save it in the output webp file.

Arguments:

  • input_png_path: The path to the input png image.
  • output_webp_path: The path to the resulting output webp file

Firstly, import the Image module from PIL. Then, take the input images: input_png image, and output_webp format empty file.

Next, create convert_png_to_webp which takes two attributes: input_png_image and output_webp files.

Next, Image.open() is used to load the image and is stored into image variable.

Then, image.save() is used to save the image to the output_webp file. 

here, .save() takes two parameters: output_webp file and format of file (".webp") 

After running the code, the input png image is converted into webp format file and is saved in the output_webp file.

 

DISCLAIMER:

1) Please note that WEBP format depends on the version of the PIL library. So, install the latest version of it. 

2) Also, store the path of the "input.png" as the path of the input image on your system and the path of the "output.webp" as the path of the output webp file on your system.

for example,

INPUT Path: "C:\Users\achau\Python Developer Internship\PNG TO WEBP\Input_image.png"

OUTPUT Path: "C:\Users\achau\Python Developer Internship\PNG TO WEBP\Output_image.webp"

 

Note: - Replace the backward slashes ("\") with the forward slashes ("/") in the path of the file.

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Amarjeet Singh Chauhan (AJchauhan04)

Download packets of source code on Coders Packet