Created
February 12, 2025 12:29
-
-
Save kms0219kms/8df208e95c3cde81e2d85c7cf8428e6d to your computer and use it in GitHub Desktop.
Image converter to WEBP using Python + Pillow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import listdir, makedirs | |
from os.path import exists, dirname, realpath, splitext, join | |
from PIL import Image | |
__dirname: str = dirname(realpath(__file__)) | |
if not exists(join(__dirname, "image-input")): | |
print('Please create a folder named "image-input" and put your images in it.') | |
exit() | |
if not exists(join(__dirname, "image-output")): | |
makedirs(join(__dirname, "image-output")) | |
for file in listdir(join(__dirname, "image-input")): | |
image = Image.open(join(__dirname, "image-input", file)) | |
filename_without_extension = splitext(file)[0] | |
image.save( | |
join(__dirname, "image-output", f"{filename_without_extension}.webp"), | |
"webp", | |
lossless=True, | |
quality=100, | |
method=6, | |
exact=True, | |
) | |
print(f"Images compressed successfully for {file}!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment