Convert image to WebP. Args: source (pathlib.Path): Path to source image Returns: pathlib.Path: path to new image
(source)
| 10 | image_types = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff'] |
| 11 | |
| 12 | def convert_to_webp(source): |
| 13 | """Convert image to WebP. |
| 14 | |
| 15 | Args: |
| 16 | source (pathlib.Path): Path to source image |
| 17 | |
| 18 | Returns: |
| 19 | pathlib.Path: path to new image |
| 20 | """ |
| 21 | destination = source.with_suffix(".webp") |
| 22 | |
| 23 | try: |
| 24 | image = Image.open(source) # Open image |
| 25 | |
| 26 | image.save(destination, format="webp") # Convert image to webp |
| 27 | os.remove(source) # Delete the original image |
| 28 | |
| 29 | return destination |
| 30 | except ValueError as e: |
| 31 | print(f'Could not convert file {source} due to error: {e}') |
| 32 | |
| 33 | def update_paths(file_path): |
| 34 | try: |