Repository navigation
Expand file tree
/
Copy pathimage_utils.py
More file actions
74 lines (56 loc) · 2.08 KB
/
Copy pathimage_utils.py
File metadata and controls
74 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests
from PIL import Image
from io import BytesIO
import base64
def load_image(path: str) -> Image.Image:
"""
Load an image from a given path.
Args:
path (str): The path to the image to load.
Returns:
Image.Image: The downloaded image as a PIL Image object.
"""
return Image.open(path)
def resize_image(image: Image.Image, max_size: int) -> Image.Image:
"""
Resize an image while maintaining its aspect ratio.
Args:
image (Image.Image): The image to resize.
max_size (int): The maximum size for either width or height.
Returns:
Image.Image: The resized image.
"""
ratio = max_size / max(image.size)
new_size: tuple[int, int] = (int(image.size[0] * ratio), int(image.size[1] * ratio))
return image.resize(new_size, Image.Resampling.LANCZOS)
def image_to_base64(image: Image.Image, format: str = "JPEG") -> str:
"""
Convert a PIL Image to a base64 encoded string.
Args:
image (Image.Image): The image to convert.
format (str): The format to save the image as. Default is 'JPEG'.
Returns:
str: The base64 encoded string of the image.
"""
buffered = BytesIO()
image.save(buffered, format=format)
return base64.b64encode(buffered.getvalue()).decode("utf-8")
def process_image(path: str, max_size: int = 1920) -> str:
"""
Download an image or GIF, resize it, and return it as a base64 encoded string.
For GIFs, it extracts a frame from the middle.
Args:
path (str): The path to the image to process.
max_size (int): The maximum size for either width or height. Default is 800.
Returns:
str: The base64 encoded string of the processed image.
"""
image = load_image(path)
if getattr(image, "is_animated", False):
frame_index = 0
image.seek(frame_index)
image = image.convert("RGB")
resized_image = resize_image(image, max_size)
f = "JPEG" if resized_image.mode == "RGB" else "PNG"
base64_image = image_to_base64(resized_image, format=f)
return f"{base64_image}"