Coverage for pyguymer3/image/image2webp.py: 5%
20 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def image2webp(
5 img,
6 webp,
7 /,
8 *,
9 exif = None,
10 lossless = False,
11 method = 6,
12 mode = "RGB",
13 quality = 100,
14 screenHeight = -1,
15 screenWidth = -1,
16):
17 """Save an image as a WEBP
19 This function accepts either a PIL Image or a file path and saves the image
20 as a WEBP.
22 Parameters
23 ----------
24 img : PIL.Image.Image or str
25 the input PIL Image or path to the input image
26 webp : str
27 the path to the output WEBP
28 exif : dict, optional
29 a dictionary of EXIF data to save in the output WEBP (default None)
30 lossless : bool, optional
31 save a lossless WEBP (default False)
32 method : int, optional
33 the method to use when saving the WEBP (default 6)
34 mode : str, optional
35 the mode of the outout WEBP (default "RGB")
36 quality : int, optional
37 the quality of the output WEBP (default 100)
38 screenHeight : int, optional
39 the height of the screen to downscale the input image to fit within,
40 currently only implemented if "img" is a str (default -1; integers less
41 than 100 imply no downscaling)
42 screenWidth : int, optional
43 the width of the screen to downscale the input image to fit within,
44 currently only implemented if "img" is a str (default -1; integers less
45 than 100 imply no downscaling)
47 Notes
48 -----
49 Copyright 2017 Thomas Guymer [1]_
51 References
52 ----------
53 .. [1] PyGuymer3, https://github.com/Guymer/PyGuymer3
54 """
56 # Import special modules ...
57 try:
58 import PIL
59 import PIL.Image
60 PIL.Image.MAX_IMAGE_PIXELS = 1024 * 1024 * 1024 # [px]
61 except:
62 raise Exception("\"PIL\" is not installed; run \"pip install --user Pillow\"") from None
64 # Import sub-functions ...
65 from .dict2exif import dict2exif
67 # Find out what the user supplied ...
68 match img:
69 case str():
70 # Open image as RGB (even if it is paletted) ...
71 with PIL.Image.open(img) as iObj:
72 tmpImg = iObj.convert("RGB")
74 # Check if the user wants to scale the image down to fit within a
75 # screen size ...
76 if screenWidth >= 100 and screenHeight >= 100:
77 # Resize image in place ...
78 tmpImg.thumbnail(
79 (screenWidth, screenHeight),
80 resample = PIL.Image.Resampling.LANCZOS,
81 )
83 # Convert it to whatever mode the user asked for ...
84 tmpImg = tmpImg.convert(mode)
85 case PIL.Image.Image():
86 # Convert image to whatever mode the user asked for ...
87 tmpImg = img.convert(mode)
88 case _:
89 # Crash ...
90 raise TypeError(f"\"img\" is an unexpected type ({repr(type(img))})") from None
92 # Save it as a WEBP ...
93 # NOTE: See https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp
94 tmpImg.save(
95 webp,
96 exif = dict2exif(exif, mode = mode),
97 lossless = lossless,
98 method = method,
99 quality = quality,
100 )