A Python script to convert images from any format to a specified target format using the Pillow
library. This script automatically processes all images in a source folder and saves them in the specified format in a target folder.
- Python 3.x
Pillow
library
Install the Pillow
library if you haven’t already:
pip install pillow
-
Set up folders:
- Define the
source_folder
as the path to the folder containing the original images. - Define the
target_folder
as the path where the converted images will be saved.
- Define the
-
Choose the target format:
- Set
target_extension
to your desired file extension (e.g.,png
,jpeg
,bmp
).
- Set
-
Run the Script:
Update the paths and target extension, then run the script to convert images:
source_folder = './images/original' target_folder = './images/converted' target_extension = 'png' # Set your target format here convert_images(source_folder, target_folder, target_extension)
The following example converts all images in the ./images/original
folder to PNG format and saves them in the ./images/converted
folder:
source_folder = './images/original'
target_folder = './images/converted'
target_extension = 'png'
convert_images(source_folder, target_folder, target_extension)
convert_images(source_folder, target_folder, extension)
: Converts images in thesource_folder
to the specifiedextension
format and saves them intarget_folder
.
The script skips unsupported or corrupted files and displays an error message without interrupting the conversion of other files.
This script is provided as-is and can be modified and distributed freely.