| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | from PIL import Image |
| | from datetime import datetime |
| | from io import BytesIO |
| | from base64 import b64encode |
| |
|
| |
|
| | def get_current_time(): |
| | |
| | start_time = datetime.now() |
| | |
| | unix_microseconds = int(start_time.timestamp() * 1_000_000) |
| | return unix_microseconds |
| |
|
| |
|
| | def is_punctuation(char): |
| | if char in [",", ",", ".", "。", "?", "?", "!", "!"]: |
| | return True |
| | return False |
| |
|
| |
|
| | def parse_sentences(sentence_fragment, content): |
| | sentences = [] |
| | current_sentence = sentence_fragment |
| | for char in content: |
| | current_sentence += char |
| | if is_punctuation(char): |
| | |
| | stripped_sentence = current_sentence |
| | if any(c.isalnum() for c in stripped_sentence): |
| | sentences.append(stripped_sentence) |
| | current_sentence = "" |
| |
|
| | remain = current_sentence |
| | return sentences, remain |
| |
|
| |
|
| | def rgb2base64jpeg(rgb_data, width, height): |
| | |
| | pil_image = Image.frombytes("RGBA", (width, height), bytes(rgb_data)) |
| | pil_image = pil_image.convert("RGB") |
| |
|
| | |
| | pil_image = resize_image_keep_aspect(pil_image, 320) |
| |
|
| | |
| | buffered = BytesIO() |
| | pil_image.save(buffered, format="JPEG") |
| | |
| |
|
| | |
| | jpeg_image_data = buffered.getvalue() |
| |
|
| | |
| | base64_encoded_image = b64encode(jpeg_image_data).decode("utf-8") |
| |
|
| | |
| | mime_type = "image/jpeg" |
| | base64_url = f"data:{mime_type};base64,{base64_encoded_image}" |
| | return base64_url |
| |
|
| |
|
| | def resize_image_keep_aspect(image, max_size=512): |
| | """ |
| | Resize an image while maintaining its aspect ratio, ensuring the larger dimension is max_size. |
| | If both dimensions are smaller than max_size, the image is not resized. |
| | |
| | :param image: A PIL Image object |
| | :param max_size: The maximum size for the larger dimension (width or height) |
| | :return: A PIL Image object (resized or original) |
| | """ |
| | |
| | width, height = image.size |
| |
|
| | |
| | if width <= max_size and height <= max_size: |
| | return image |
| |
|
| | |
| | aspect_ratio = width / height |
| |
|
| | |
| | if width > height: |
| | new_width = max_size |
| | new_height = int(max_size / aspect_ratio) |
| | else: |
| | new_height = max_size |
| | new_width = int(max_size * aspect_ratio) |
| |
|
| | |
| | resized_image = image.resize((new_width, new_height)) |
| |
|
| | return resized_image |
| |
|