image.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. from __future__ import annotations
  2. import os
  3. import re
  4. import io
  5. import time
  6. import uuid
  7. import base64
  8. import asyncio
  9. from io import BytesIO
  10. from pathlib import Path
  11. from aiohttp import ClientSession, ClientError
  12. try:
  13. from PIL.Image import open as open_image, new as new_image
  14. from PIL.Image import FLIP_LEFT_RIGHT, ROTATE_180, ROTATE_270, ROTATE_90
  15. has_requirements = True
  16. except ImportError:
  17. has_requirements = False
  18. from .typing import ImageType, Union, Image, Optional, Cookies
  19. from .errors import MissingRequirementsError
  20. from .providers.response import ImageResponse, ImagePreview
  21. from .requests.aiohttp import get_connector
  22. from . import debug
  23. ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'}
  24. EXTENSIONS_MAP: dict[str, str] = {
  25. "image/png": "png",
  26. "image/jpeg": "jpg",
  27. "image/gif": "gif",
  28. "image/webp": "webp",
  29. }
  30. # Define the directory for generated images
  31. images_dir = "./generated_images"
  32. def to_image(image: ImageType, is_svg: bool = False) -> Image:
  33. """
  34. Converts the input image to a PIL Image object.
  35. Args:
  36. image (Union[str, bytes, Image]): The input image.
  37. Returns:
  38. Image: The converted PIL Image object.
  39. """
  40. if not has_requirements:
  41. raise MissingRequirementsError('Install "pillow" package for images')
  42. if isinstance(image, str) and image.startswith("data:"):
  43. is_data_uri_an_image(image)
  44. image = extract_data_uri(image)
  45. if is_svg:
  46. try:
  47. import cairosvg
  48. except ImportError:
  49. raise MissingRequirementsError('Install "cairosvg" package for svg images')
  50. if not isinstance(image, bytes):
  51. image = image.read()
  52. buffer = BytesIO()
  53. cairosvg.svg2png(image, write_to=buffer)
  54. return open_image(buffer)
  55. if isinstance(image, bytes):
  56. is_accepted_format(image)
  57. return open_image(BytesIO(image))
  58. elif not isinstance(image, Image):
  59. image = open_image(image)
  60. image.load()
  61. return image
  62. return image
  63. def is_allowed_extension(filename: str) -> bool:
  64. """
  65. Checks if the given filename has an allowed extension.
  66. Args:
  67. filename (str): The filename to check.
  68. Returns:
  69. bool: True if the extension is allowed, False otherwise.
  70. """
  71. return '.' in filename and \
  72. filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
  73. def is_data_uri_an_image(data_uri: str) -> bool:
  74. """
  75. Checks if the given data URI represents an image.
  76. Args:
  77. data_uri (str): The data URI to check.
  78. Raises:
  79. ValueError: If the data URI is invalid or the image format is not allowed.
  80. """
  81. # Check if the data URI starts with 'data:image' and contains an image format (e.g., jpeg, png, gif)
  82. if not re.match(r'data:image/(\w+);base64,', data_uri):
  83. raise ValueError("Invalid data URI image.")
  84. # Extract the image format from the data URI
  85. image_format = re.match(r'data:image/(\w+);base64,', data_uri).group(1).lower()
  86. # Check if the image format is one of the allowed formats (jpg, jpeg, png, gif)
  87. if image_format not in ALLOWED_EXTENSIONS and image_format != "svg+xml":
  88. raise ValueError("Invalid image format (from mime file type).")
  89. def is_accepted_format(binary_data: bytes) -> str:
  90. """
  91. Checks if the given binary data represents an image with an accepted format.
  92. Args:
  93. binary_data (bytes): The binary data to check.
  94. Raises:
  95. ValueError: If the image format is not allowed.
  96. """
  97. if binary_data.startswith(b'\xFF\xD8\xFF'):
  98. return "image/jpeg"
  99. elif binary_data.startswith(b'\x89PNG\r\n\x1a\n'):
  100. return "image/png"
  101. elif binary_data.startswith(b'GIF87a') or binary_data.startswith(b'GIF89a'):
  102. return "image/gif"
  103. elif binary_data.startswith(b'\x89JFIF') or binary_data.startswith(b'JFIF\x00'):
  104. return "image/jpeg"
  105. elif binary_data.startswith(b'\xFF\xD8'):
  106. return "image/jpeg"
  107. elif binary_data.startswith(b'RIFF') and binary_data[8:12] == b'WEBP':
  108. return "image/webp"
  109. else:
  110. raise ValueError("Invalid image format (from magic code).")
  111. def extract_data_uri(data_uri: str) -> bytes:
  112. """
  113. Extracts the binary data from the given data URI.
  114. Args:
  115. data_uri (str): The data URI.
  116. Returns:
  117. bytes: The extracted binary data.
  118. """
  119. data = data_uri.split(",")[-1]
  120. data = base64.b64decode(data)
  121. return data
  122. def get_orientation(image: Image) -> int:
  123. """
  124. Gets the orientation of the given image.
  125. Args:
  126. image (Image): The image.
  127. Returns:
  128. int: The orientation value.
  129. """
  130. exif_data = image.getexif() if hasattr(image, 'getexif') else image._getexif()
  131. if exif_data is not None:
  132. orientation = exif_data.get(274) # 274 corresponds to the orientation tag in EXIF
  133. if orientation is not None:
  134. return orientation
  135. def process_image(image: Image, new_width: int, new_height: int) -> Image:
  136. """
  137. Processes the given image by adjusting its orientation and resizing it.
  138. Args:
  139. image (Image): The image to process.
  140. new_width (int): The new width of the image.
  141. new_height (int): The new height of the image.
  142. Returns:
  143. Image: The processed image.
  144. """
  145. # Fix orientation
  146. orientation = get_orientation(image)
  147. if orientation:
  148. if orientation > 4:
  149. image = image.transpose(FLIP_LEFT_RIGHT)
  150. if orientation in [3, 4]:
  151. image = image.transpose(ROTATE_180)
  152. if orientation in [5, 6]:
  153. image = image.transpose(ROTATE_270)
  154. if orientation in [7, 8]:
  155. image = image.transpose(ROTATE_90)
  156. # Resize image
  157. image.thumbnail((new_width, new_height))
  158. # Remove transparency
  159. if image.mode == "RGBA":
  160. image.load()
  161. white = new_image('RGB', image.size, (255, 255, 255))
  162. white.paste(image, mask=image.split()[-1])
  163. return white
  164. # Convert to RGB for jpg format
  165. elif image.mode != "RGB":
  166. image = image.convert("RGB")
  167. return image
  168. def to_bytes(image: ImageType) -> bytes:
  169. """
  170. Converts the given image to bytes.
  171. Args:
  172. image (ImageType): The image to convert.
  173. Returns:
  174. bytes: The image as bytes.
  175. """
  176. if isinstance(image, bytes):
  177. return image
  178. elif isinstance(image, str) and image.startswith("data:"):
  179. is_data_uri_an_image(image)
  180. return extract_data_uri(image)
  181. elif isinstance(image, Image):
  182. bytes_io = BytesIO()
  183. image.save(bytes_io, image.format)
  184. image.seek(0)
  185. return bytes_io.getvalue()
  186. elif isinstance(image, (str, os.PathLike)):
  187. return Path(image).read_bytes()
  188. elif isinstance(image, Path):
  189. return image.read_bytes()
  190. else:
  191. try:
  192. image.seek(0)
  193. except (AttributeError, io.UnsupportedOperation):
  194. pass
  195. return image.read()
  196. def to_data_uri(image: ImageType) -> str:
  197. if not isinstance(image, str):
  198. data = to_bytes(image)
  199. data_base64 = base64.b64encode(data).decode()
  200. return f"data:{is_accepted_format(data)};base64,{data_base64}"
  201. return image
  202. # Function to ensure the images directory exists
  203. def ensure_images_dir():
  204. os.makedirs(images_dir, exist_ok=True)
  205. async def copy_images(
  206. images: list[str],
  207. cookies: Optional[Cookies] = None,
  208. proxy: Optional[str] = None
  209. ) -> list[str]:
  210. ensure_images_dir()
  211. async with ClientSession(
  212. connector=get_connector(proxy=proxy),
  213. cookies=cookies
  214. ) as session:
  215. async def copy_image(image: str) -> str:
  216. target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}")
  217. if image.startswith("data:"):
  218. with open(target, "wb") as f:
  219. f.write(extract_data_uri(image))
  220. else:
  221. try:
  222. async with session.get(image) as response:
  223. response.raise_for_status()
  224. with open(target, "wb") as f:
  225. async for chunk in response.content.iter_chunked(4096):
  226. f.write(chunk)
  227. except ClientError as e:
  228. debug.log(f"copy_images failed: {e.__class__.__name__}: {e}")
  229. return image
  230. with open(target, "rb") as f:
  231. extension = is_accepted_format(f.read(12)).split("/")[-1]
  232. extension = "jpg" if extension == "jpeg" else extension
  233. new_target = f"{target}.{extension}"
  234. os.rename(target, new_target)
  235. return f"/images/{os.path.basename(new_target)}"
  236. return await asyncio.gather(*[copy_image(image) for image in images])
  237. class ImageDataResponse():
  238. def __init__(
  239. self,
  240. images: Union[str, list],
  241. alt: str,
  242. ):
  243. self.images = images
  244. self.alt = alt
  245. def get_list(self) -> list[str]:
  246. return [self.images] if isinstance(self.images, str) else self.images
  247. class ImageRequest:
  248. def __init__(
  249. self,
  250. options: dict = {}
  251. ):
  252. self.options = options
  253. def get(self, key: str):
  254. return self.options.get(key)