2024-01-26 09:54:13 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-20 21:58:16 +03:00
|
|
|
import os
|
2024-01-13 17:37:36 +03:00
|
|
|
import re
|
2024-11-20 21:58:16 +03:00
|
|
|
import time
|
|
|
|
import uuid
|
2024-01-13 17:37:36 +03:00
|
|
|
from io import BytesIO
|
|
|
|
import base64
|
2024-11-20 21:58:16 +03:00
|
|
|
import asyncio
|
2024-12-06 23:54:13 +03:00
|
|
|
from aiohttp import ClientSession, ClientError
|
2024-01-26 09:54:13 +03:00
|
|
|
try:
|
2024-01-29 20:14:46 +03:00
|
|
|
from PIL.Image import open as open_image, new as new_image
|
2024-01-26 09:54:13 +03:00
|
|
|
from PIL.Image import FLIP_LEFT_RIGHT, ROTATE_180, ROTATE_270, ROTATE_90
|
|
|
|
has_requirements = True
|
|
|
|
except ImportError:
|
|
|
|
has_requirements = False
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-11-20 21:58:16 +03:00
|
|
|
from .typing import ImageType, Union, Image, Optional, Cookies
|
2024-01-26 09:54:13 +03:00
|
|
|
from .errors import MissingRequirementsError
|
2024-11-20 21:58:16 +03:00
|
|
|
from .providers.response import ResponseType
|
|
|
|
from .requests.aiohttp import get_connector
|
2024-12-06 23:54:13 +03:00
|
|
|
from . import debug
|
2024-01-13 17:37:36 +03:00
|
|
|
|
2024-01-23 03:47:55 +03:00
|
|
|
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'}
|
2024-01-13 17:37:36 +03:00
|
|
|
|
2024-05-21 23:58:56 +03:00
|
|
|
EXTENSIONS_MAP: dict[str, str] = {
|
|
|
|
"image/png": "png",
|
|
|
|
"image/jpeg": "jpg",
|
|
|
|
"image/gif": "gif",
|
|
|
|
"image/webp": "webp",
|
|
|
|
}
|
|
|
|
|
2024-11-20 21:58:16 +03:00
|
|
|
# Define the directory for generated images
|
|
|
|
images_dir = "./generated_images"
|
|
|
|
|
2024-11-23 02:17:35 +03:00
|
|
|
def fix_url(url: str) -> str:
|
2024-11-15 12:10:04 +03:00
|
|
|
""" replace ' ' by '+' (to be markdown compliant)"""
|
|
|
|
return url.replace(" ","+")
|
|
|
|
|
2024-11-23 02:17:35 +03:00
|
|
|
def fix_title(title: str) -> str:
|
|
|
|
if title:
|
|
|
|
return title.replace("\n", "").replace('"', '')
|
|
|
|
|
2024-01-26 09:54:13 +03:00
|
|
|
def to_image(image: ImageType, is_svg: bool = False) -> Image:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Converts the input image to a PIL Image object.
|
|
|
|
|
|
|
|
Args:
|
2024-01-26 09:54:13 +03:00
|
|
|
image (Union[str, bytes, Image]): The input image.
|
2024-01-14 09:45:41 +03:00
|
|
|
|
|
|
|
Returns:
|
2024-01-26 09:54:13 +03:00
|
|
|
Image: The converted PIL Image object.
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
2024-01-26 09:54:13 +03:00
|
|
|
if not has_requirements:
|
|
|
|
raise MissingRequirementsError('Install "pillow" package for images')
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-01-29 20:14:46 +03:00
|
|
|
if isinstance(image, str):
|
|
|
|
is_data_uri_an_image(image)
|
|
|
|
image = extract_data_uri(image)
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-01-23 03:47:55 +03:00
|
|
|
if is_svg:
|
|
|
|
try:
|
|
|
|
import cairosvg
|
|
|
|
except ImportError:
|
2024-01-26 09:54:13 +03:00
|
|
|
raise MissingRequirementsError('Install "cairosvg" package for svg images')
|
2024-01-23 03:47:55 +03:00
|
|
|
if not isinstance(image, bytes):
|
|
|
|
image = image.read()
|
|
|
|
buffer = BytesIO()
|
|
|
|
cairosvg.svg2png(image, write_to=buffer)
|
2024-01-26 09:54:13 +03:00
|
|
|
return open_image(buffer)
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
if isinstance(image, bytes):
|
|
|
|
is_accepted_format(image)
|
2024-01-26 09:54:13 +03:00
|
|
|
return open_image(BytesIO(image))
|
|
|
|
elif not isinstance(image, Image):
|
|
|
|
image = open_image(image)
|
2024-02-09 05:31:05 +03:00
|
|
|
image.load()
|
|
|
|
return image
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
return image
|
|
|
|
|
2024-01-14 09:45:41 +03:00
|
|
|
def is_allowed_extension(filename: str) -> bool:
|
|
|
|
"""
|
|
|
|
Checks if the given filename has an allowed extension.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filename (str): The filename to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the extension is allowed, False otherwise.
|
|
|
|
"""
|
2024-01-13 17:37:36 +03:00
|
|
|
return '.' in filename and \
|
|
|
|
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
|
|
|
|
def is_data_uri_an_image(data_uri: str) -> bool:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Checks if the given data URI represents an image.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data_uri (str): The data URI to check.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: If the data URI is invalid or the image format is not allowed.
|
|
|
|
"""
|
2024-01-13 17:37:36 +03:00
|
|
|
# Check if the data URI starts with 'data:image' and contains an image format (e.g., jpeg, png, gif)
|
|
|
|
if not re.match(r'data:image/(\w+);base64,', data_uri):
|
|
|
|
raise ValueError("Invalid data URI image.")
|
2024-01-14 09:45:41 +03:00
|
|
|
# Extract the image format from the data URI
|
2024-01-29 20:14:46 +03:00
|
|
|
image_format = re.match(r'data:image/(\w+);base64,', data_uri).group(1).lower()
|
2024-01-13 17:37:36 +03:00
|
|
|
# Check if the image format is one of the allowed formats (jpg, jpeg, png, gif)
|
2024-01-29 20:14:46 +03:00
|
|
|
if image_format not in ALLOWED_EXTENSIONS and image_format != "svg+xml":
|
2024-01-13 17:37:36 +03:00
|
|
|
raise ValueError("Invalid image format (from mime file type).")
|
|
|
|
|
2024-04-22 02:27:48 +03:00
|
|
|
def is_accepted_format(binary_data: bytes) -> str:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Checks if the given binary data represents an image with an accepted format.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
binary_data (bytes): The binary data to check.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: If the image format is not allowed.
|
|
|
|
"""
|
2024-01-13 17:37:36 +03:00
|
|
|
if binary_data.startswith(b'\xFF\xD8\xFF'):
|
2024-02-23 13:33:38 +03:00
|
|
|
return "image/jpeg"
|
2024-01-13 17:37:36 +03:00
|
|
|
elif binary_data.startswith(b'\x89PNG\r\n\x1a\n'):
|
2024-02-23 13:33:38 +03:00
|
|
|
return "image/png"
|
2024-01-13 17:37:36 +03:00
|
|
|
elif binary_data.startswith(b'GIF87a') or binary_data.startswith(b'GIF89a'):
|
2024-02-23 13:33:38 +03:00
|
|
|
return "image/gif"
|
2024-01-13 17:37:36 +03:00
|
|
|
elif binary_data.startswith(b'\x89JFIF') or binary_data.startswith(b'JFIF\x00'):
|
2024-02-23 13:33:38 +03:00
|
|
|
return "image/jpeg"
|
2024-01-13 17:37:36 +03:00
|
|
|
elif binary_data.startswith(b'\xFF\xD8'):
|
2024-02-23 13:33:38 +03:00
|
|
|
return "image/jpeg"
|
2024-01-13 17:37:36 +03:00
|
|
|
elif binary_data.startswith(b'RIFF') and binary_data[8:12] == b'WEBP':
|
2024-02-23 13:33:38 +03:00
|
|
|
return "image/webp"
|
2024-01-13 17:37:36 +03:00
|
|
|
else:
|
|
|
|
raise ValueError("Invalid image format (from magic code).")
|
2024-01-14 09:45:41 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
def extract_data_uri(data_uri: str) -> bytes:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Extracts the binary data from the given data URI.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data_uri (str): The data URI.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bytes: The extracted binary data.
|
|
|
|
"""
|
2024-11-17 13:06:37 +03:00
|
|
|
data = data_uri.split(",")[-1]
|
2024-01-13 17:37:36 +03:00
|
|
|
data = base64.b64decode(data)
|
|
|
|
return data
|
|
|
|
|
2024-01-26 09:54:13 +03:00
|
|
|
def get_orientation(image: Image) -> int:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Gets the orientation of the given image.
|
|
|
|
|
|
|
|
Args:
|
2024-01-26 09:54:13 +03:00
|
|
|
image (Image): The image.
|
2024-01-14 09:45:41 +03:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
int: The orientation value.
|
|
|
|
"""
|
2024-01-13 17:37:36 +03:00
|
|
|
exif_data = image.getexif() if hasattr(image, 'getexif') else image._getexif()
|
|
|
|
if exif_data is not None:
|
2024-01-21 04:20:23 +03:00
|
|
|
orientation = exif_data.get(274) # 274 corresponds to the orientation tag in EXIF
|
2024-01-13 17:37:36 +03:00
|
|
|
if orientation is not None:
|
|
|
|
return orientation
|
|
|
|
|
2024-02-11 05:33:02 +03:00
|
|
|
def process_image(image: Image, new_width: int, new_height: int) -> Image:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Processes the given image by adjusting its orientation and resizing it.
|
|
|
|
|
|
|
|
Args:
|
2024-02-11 05:33:02 +03:00
|
|
|
image (Image): The image to process.
|
2024-01-14 09:45:41 +03:00
|
|
|
new_width (int): The new width of the image.
|
|
|
|
new_height (int): The new height of the image.
|
|
|
|
|
|
|
|
Returns:
|
2024-01-26 09:54:13 +03:00
|
|
|
Image: The processed image.
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
2024-01-23 18:48:52 +03:00
|
|
|
# Fix orientation
|
2024-02-11 05:33:02 +03:00
|
|
|
orientation = get_orientation(image)
|
2024-01-13 17:37:36 +03:00
|
|
|
if orientation:
|
|
|
|
if orientation > 4:
|
2024-02-11 05:33:02 +03:00
|
|
|
image = image.transpose(FLIP_LEFT_RIGHT)
|
2024-01-13 17:37:36 +03:00
|
|
|
if orientation in [3, 4]:
|
2024-02-11 05:33:02 +03:00
|
|
|
image = image.transpose(ROTATE_180)
|
2024-01-13 17:37:36 +03:00
|
|
|
if orientation in [5, 6]:
|
2024-02-11 05:33:02 +03:00
|
|
|
image = image.transpose(ROTATE_270)
|
2024-01-13 17:37:36 +03:00
|
|
|
if orientation in [7, 8]:
|
2024-02-11 05:33:02 +03:00
|
|
|
image = image.transpose(ROTATE_90)
|
2024-01-23 18:48:52 +03:00
|
|
|
# Resize image
|
2024-02-11 05:33:02 +03:00
|
|
|
image.thumbnail((new_width, new_height))
|
2024-01-23 18:48:52 +03:00
|
|
|
# Remove transparency
|
2024-02-11 05:33:02 +03:00
|
|
|
if image.mode == "RGBA":
|
|
|
|
image.load()
|
|
|
|
white = new_image('RGB', image.size, (255, 255, 255))
|
|
|
|
white.paste(image, mask=image.split()[-1])
|
2024-01-23 18:48:52 +03:00
|
|
|
return white
|
2024-03-11 04:41:59 +03:00
|
|
|
# Convert to RGB for jpg format
|
2024-02-11 05:33:02 +03:00
|
|
|
elif image.mode != "RGB":
|
|
|
|
image = image.convert("RGB")
|
|
|
|
return image
|
2024-01-14 09:45:41 +03:00
|
|
|
|
2024-01-26 09:54:13 +03:00
|
|
|
def to_base64_jpg(image: Image, compression_rate: float) -> str:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Converts the given image to a base64-encoded string.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
image (Image.Image): The image to convert.
|
|
|
|
compression_rate (float): The compression rate (0.0 to 1.0).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The base64-encoded image.
|
|
|
|
"""
|
2024-01-13 17:37:36 +03:00
|
|
|
output_buffer = BytesIO()
|
|
|
|
image.save(output_buffer, format="JPEG", quality=int(compression_rate * 100))
|
|
|
|
return base64.b64encode(output_buffer.getvalue()).decode()
|
|
|
|
|
2024-02-11 11:26:02 +03:00
|
|
|
def format_images_markdown(images: Union[str, list], alt: str, preview: Union[str, list] = None) -> str:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Formats the given images as a markdown string.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
images: The images to format.
|
2024-01-21 04:20:23 +03:00
|
|
|
alt (str): The alt for the images.
|
2024-01-14 09:45:41 +03:00
|
|
|
preview (str, optional): The preview URL format. Defaults to "{image}?w=200&h=200".
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The formatted markdown string.
|
|
|
|
"""
|
2024-01-21 04:20:23 +03:00
|
|
|
if isinstance(images, str):
|
2024-11-23 02:17:35 +03:00
|
|
|
result = f"[![{fix_title(alt)}]({fix_url(preview.replace('{image}', images) if preview else images)})]({fix_url(images)})"
|
2024-01-13 17:37:36 +03:00
|
|
|
else:
|
2024-02-11 11:26:02 +03:00
|
|
|
if not isinstance(preview, list):
|
|
|
|
preview = [preview.replace('{image}', image) if preview else image for image in images]
|
2024-02-22 02:16:58 +03:00
|
|
|
result = "\n".join(
|
2024-11-23 02:17:35 +03:00
|
|
|
f"[![#{idx+1} {fix_title(alt)}]({fix_url(preview[idx])})]({fix_url(image)})"
|
2024-04-21 08:22:59 +03:00
|
|
|
for idx, image in enumerate(images)
|
2024-02-22 02:16:58 +03:00
|
|
|
)
|
2024-01-13 17:37:36 +03:00
|
|
|
start_flag = "<!-- generated images start -->\n"
|
|
|
|
end_flag = "<!-- generated images end -->\n"
|
2024-02-22 02:16:58 +03:00
|
|
|
return f"\n{start_flag}{result}\n{end_flag}\n"
|
2024-01-13 17:37:36 +03:00
|
|
|
|
2024-02-09 00:02:52 +03:00
|
|
|
def to_bytes(image: ImageType) -> bytes:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Converts the given image to bytes.
|
|
|
|
|
|
|
|
Args:
|
2024-02-09 00:02:52 +03:00
|
|
|
image (ImageType): The image to convert.
|
2024-01-14 09:45:41 +03:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
bytes: The image as bytes.
|
|
|
|
"""
|
2024-02-09 00:02:52 +03:00
|
|
|
if isinstance(image, bytes):
|
|
|
|
return image
|
|
|
|
elif isinstance(image, str):
|
|
|
|
is_data_uri_an_image(image)
|
|
|
|
return extract_data_uri(image)
|
|
|
|
elif isinstance(image, Image):
|
|
|
|
bytes_io = BytesIO()
|
|
|
|
image.save(bytes_io, image.format)
|
|
|
|
image.seek(0)
|
|
|
|
return bytes_io.getvalue()
|
|
|
|
else:
|
|
|
|
return image.read()
|
2024-01-13 17:37:36 +03:00
|
|
|
|
2024-04-22 02:27:48 +03:00
|
|
|
def to_data_uri(image: ImageType) -> str:
|
|
|
|
if not isinstance(image, str):
|
|
|
|
data = to_bytes(image)
|
|
|
|
data_base64 = base64.b64encode(data).decode()
|
|
|
|
return f"data:{is_accepted_format(data)};base64,{data_base64}"
|
|
|
|
return image
|
|
|
|
|
2024-11-20 21:58:16 +03:00
|
|
|
# Function to ensure the images directory exists
|
|
|
|
def ensure_images_dir():
|
|
|
|
if not os.path.exists(images_dir):
|
|
|
|
os.makedirs(images_dir)
|
|
|
|
|
2024-12-06 23:54:13 +03:00
|
|
|
async def copy_images(
|
|
|
|
images: list[str],
|
|
|
|
cookies: Optional[Cookies] = None,
|
|
|
|
proxy: Optional[str] = None
|
|
|
|
):
|
2024-11-20 21:58:16 +03:00
|
|
|
ensure_images_dir()
|
|
|
|
async with ClientSession(
|
2024-12-06 23:54:13 +03:00
|
|
|
connector=get_connector(proxy=proxy),
|
2024-11-20 21:58:16 +03:00
|
|
|
cookies=cookies
|
|
|
|
) as session:
|
|
|
|
async def copy_image(image: str) -> str:
|
|
|
|
target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}")
|
|
|
|
if image.startswith("data:"):
|
|
|
|
with open(target, "wb") as f:
|
|
|
|
f.write(extract_data_uri(image))
|
|
|
|
else:
|
2024-12-06 23:54:13 +03:00
|
|
|
try:
|
|
|
|
async with session.get(image) as response:
|
|
|
|
response.raise_for_status()
|
|
|
|
with open(target, "wb") as f:
|
|
|
|
async for chunk in response.content.iter_chunked(4096):
|
|
|
|
f.write(chunk)
|
|
|
|
except ClientError as e:
|
|
|
|
debug.log(f"copy_images failed: {e.__class__.__name__}: {e}")
|
|
|
|
return image
|
2024-11-20 21:58:16 +03:00
|
|
|
with open(target, "rb") as f:
|
|
|
|
extension = is_accepted_format(f.read(12)).split("/")[-1]
|
|
|
|
extension = "jpg" if extension == "jpeg" else extension
|
|
|
|
new_target = f"{target}.{extension}"
|
|
|
|
os.rename(target, new_target)
|
|
|
|
return f"/images/{os.path.basename(new_target)}"
|
|
|
|
|
|
|
|
return await asyncio.gather(*[copy_image(image) for image in images])
|
|
|
|
|
|
|
|
class ImageResponse(ResponseType):
|
2024-01-13 17:37:36 +03:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
images: Union[str, list],
|
|
|
|
alt: str,
|
|
|
|
options: dict = {}
|
|
|
|
):
|
|
|
|
self.images = images
|
|
|
|
self.alt = alt
|
|
|
|
self.options = options
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
def __str__(self) -> str:
|
2024-01-29 20:14:46 +03:00
|
|
|
return format_images_markdown(self.images, self.alt, self.get("preview"))
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-01-13 17:37:36 +03:00
|
|
|
def get(self, key: str):
|
2024-01-26 09:54:13 +03:00
|
|
|
return self.options.get(key)
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-03-11 04:41:59 +03:00
|
|
|
def get_list(self) -> list[str]:
|
|
|
|
return [self.images] if isinstance(self.images, str) else self.images
|
|
|
|
|
2024-04-21 08:22:59 +03:00
|
|
|
class ImagePreview(ImageResponse):
|
|
|
|
def __str__(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
return super().__str__()
|
|
|
|
|
2024-05-18 08:37:37 +03:00
|
|
|
class ImageDataResponse():
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
images: Union[str, list],
|
|
|
|
alt: str,
|
|
|
|
):
|
|
|
|
self.images = images
|
|
|
|
self.alt = alt
|
|
|
|
|
|
|
|
def get_list(self) -> list[str]:
|
|
|
|
return [self.images] if isinstance(self.images, str) else self.images
|
|
|
|
|
2024-01-29 20:14:46 +03:00
|
|
|
class ImageRequest:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
options: dict = {}
|
|
|
|
):
|
|
|
|
self.options = options
|
2024-02-22 02:16:58 +03:00
|
|
|
|
2024-01-29 20:14:46 +03:00
|
|
|
def get(self, key: str):
|
2024-11-15 12:10:04 +03:00
|
|
|
return self.options.get(key)
|