Python batch converts images to webp
Python 3.8.10 is required, I don’t want to use online conversion awa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| import os
import sys
from PIL import Image
def convert_to_webp(input_file, output_file, quality=80):
try:
with Image.open(input_file) as im:
im.save(output_file, "webp", quality=quality)
print(f"Converted: {input_file} => {output_file}")
except Exception as e:
print(f"Error converting file: {input_file}")
print(str(e))
def process_folder(folder_path):
for root, dirs, files in os.walk(folder_path):
for filename in files:
if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png']):
input_file = os.path.join(root, filename)
output_file = os.path.splitext(input_file)[0] + ".webp"
convert_to_webp(input_file, output_file)
if __name__ == "__main__":
folder_path = os.path.dirname(sys.argv[0])
process_folder(folder_path)
|
This code is used to batch convert JPG, JPEG and PNG images in a folder to WEBP format.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| import os # Process files and directories
import sys # Get command line parameters
from PIL import Image # Process images
def convert_to_webp(input_file, output_file, quality=80):
# Function used to convert an image file
# input_file: input image file path
# output_file: output webp file path
# quality: webp file quality, value range 1-100, default value 80
try:
# Open input image file
with Image.open(input_file) as im:
# Save in webp format
im.save(output_file, "webp", quality=quality)
print(f"Converted: {input_file} => {output_file}")
except Exception as e:
# Catch exceptions
print(f"Error converting file: {input_file}")
print(str(e))
def process_folder(folder_path):
# Function used to process the entire folder
# folder_path: folder path
for root, dirs, files in os.walk(folder_path):
for filename in files:
# Check if the file suffix is jpg/jpeg/png
if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png']):
input_file = os.path.join(root, filename)
output_file = os.path.splitext(input_file)[0] + ".webp"
# Call the conversion function
convert_to_webp(input_file, output_file)
if __name__ == "__main__":
# Get the current script path
folder_path = os.path.dirname(sys.argv[0])
process_folder(folder_path)
|
Libraries to be installed
Here are the steps to install the Pillow library:
Make sure you have Python installed.
Open the command prompt (Windows) or terminal (Mac/Linux).
Enter the following command to install Pillow:
- After the installation is successful, you can import the Pillow library for image processing:
- If you encounter permission issues, you can use sudo or add administrator privileges to install:
1
| sudo pip install Pillow
|
1
| pip install Pillow --user
|
In Windows, you sometimes need to upgrade pip using the pip install -U Pillow command to install Pillow.
Pillow has some dependencies, so you may also need to install these during installation.
Example installation command:
The main step is to use pip to install the Pillow library, and then you can import the PIL package in Python code and use Pillow.
Postscript
Perfectly achieve the requirements of converting webp