请注意,本文编写于 1052 天前,最后修改于 360 天前,其中某些信息可能已经过时。
https://www.webucator.com/tutorial/using-python-to-convert-images-to-webp/
from pathlib import Path
from PIL import Image
def convert_to_webp(source):
"""Convert image to webp.
Args:
source (pathlib.Path): Path to source image
Returns:
pathlib.Path: path to new image
"""
destination = source.with_suffix(".webp")
image = Image.open(source) # Open image
image.save(destination, format="webp") # Convert image to webp
return destination
def main():
paths = Path("pic").glob("**/*.jpg")
for path in paths:
webp_path = convert_to_webp(path)
print(webp_path)
main()
CC BY-ND
This license enables reusers to copy and distribute the material in any medium or format in unadapted form only, and only so long as attribution is given to the creator. The license allows for commercial use.