image.py 10 KB

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