Skip to content

Instantly share code, notes, and snippets.

@kms0219kms
Created February 12, 2025 12:29
Show Gist options
  • Save kms0219kms/8df208e95c3cde81e2d85c7cf8428e6d to your computer and use it in GitHub Desktop.
Save kms0219kms/8df208e95c3cde81e2d85c7cf8428e6d to your computer and use it in GitHub Desktop.
Image converter to WEBP using Python + Pillow
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