image.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. from __future__ import annotations
  2. import os
  3. import re
  4. import time
  5. import uuid
  6. from io import BytesIO
  7. import base64
  8. import asyncio
  9. from aiohttp import ClientSession
  10. try:
  11. from PIL.Image import open as open_image, new as new_image
  12. from PIL.Image import FLIP_LEFT_RIGHT, ROTATE_180, ROTATE_270, ROTATE_90
  13. has_requirements = True
  14. except ImportError:
  15. has_requirements = False
  16. from .typing import ImageType, Union, Image, Optional, Cookies
  17. from .errors import MissingRequirementsError
  18. from .providers.response import ResponseType
  19. from .requests.aiohttp import get_connector
  20. ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp', 'svg'}
  21. EXTENSIONS_MAP: dict[str, str] = {
  22. "image/png": "png",
  23. "image/jpeg": "jpg",
  24. "image/gif": "gif",
  25. "image/webp": "webp",
  26. }
  27. # Define the directory for generated images
  28. images_dir = "./generated_images"
  29. def fix_url(url:str) -> str:
  30. """ replace ' ' by '+' (to be markdown compliant)"""
  31. return url.replace(" ","+")
  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):
  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_base64_jpg(image: Image, compression_rate: float) -> str:
  169. """
  170. Converts the given image to a base64-encoded string.
  171. Args:
  172. image (Image.Image): The image to convert.
  173. compression_rate (float): The compression rate (0.0 to 1.0).
  174. Returns:
  175. str: The base64-encoded image.
  176. """
  177. output_buffer = BytesIO()
  178. image.save(output_buffer, format="JPEG", quality=int(compression_rate * 100))
  179. return base64.b64encode(output_buffer.getvalue()).decode()
  180. def format_images_markdown(images: Union[str, list], alt: str, preview: Union[str, list] = None) -> str:
  181. """
  182. Formats the given images as a markdown string.
  183. Args:
  184. images: The images to format.
  185. alt (str): The alt for the images.
  186. preview (str, optional): The preview URL format. Defaults to "{image}?w=200&h=200".
  187. Returns:
  188. str: The formatted markdown string.
  189. """
  190. if isinstance(images, str):
  191. result = f"[![{alt}]({fix_url(preview.replace('{image}', images) if preview else images)})]({fix_url(images)})"
  192. else:
  193. if not isinstance(preview, list):
  194. preview = [preview.replace('{image}', image) if preview else image for image in images]
  195. result = "\n".join(
  196. f"[![#{idx+1} {alt}]({fix_url(preview[idx])})]({fix_url(image)})"
  197. for idx, image in enumerate(images)
  198. )
  199. start_flag = "<!-- generated images start -->\n"
  200. end_flag = "<!-- generated images end -->\n"
  201. return f"\n{start_flag}{result}\n{end_flag}\n"
  202. def to_bytes(image: ImageType) -> bytes:
  203. """
  204. Converts the given image to bytes.
  205. Args:
  206. image (ImageType): The image to convert.
  207. Returns:
  208. bytes: The image as bytes.
  209. """
  210. if isinstance(image, bytes):
  211. return image
  212. elif isinstance(image, str):
  213. is_data_uri_an_image(image)
  214. return extract_data_uri(image)
  215. elif isinstance(image, Image):
  216. bytes_io = BytesIO()
  217. image.save(bytes_io, image.format)
  218. image.seek(0)
  219. return bytes_io.getvalue()
  220. else:
  221. return image.read()
  222. def to_data_uri(image: ImageType) -> str:
  223. if not isinstance(image, str):
  224. data = to_bytes(image)
  225. data_base64 = base64.b64encode(data).decode()
  226. return f"data:{is_accepted_format(data)};base64,{data_base64}"
  227. return image
  228. # Function to ensure the images directory exists
  229. def ensure_images_dir():
  230. if not os.path.exists(images_dir):
  231. os.makedirs(images_dir)
  232. async def copy_images(images: list[str], cookies: Optional[Cookies] = None, proxy: Optional[str] = None):
  233. ensure_images_dir()
  234. async with ClientSession(
  235. connector=get_connector(
  236. proxy=os.environ.get("G4F_PROXY") if proxy is None else proxy
  237. ),
  238. cookies=cookies
  239. ) as session:
  240. async def copy_image(image: str) -> str:
  241. target = os.path.join(images_dir, f"{int(time.time())}_{str(uuid.uuid4())}")
  242. if image.startswith("data:"):
  243. with open(target, "wb") as f:
  244. f.write(extract_data_uri(image))
  245. else:
  246. async with session.get(image) as response:
  247. with open(target, "wb") as f:
  248. async for chunk in response.content.iter_chunked(4096):
  249. f.write(chunk)
  250. with open(target, "rb") as f:
  251. extension = is_accepted_format(f.read(12)).split("/")[-1]
  252. extension = "jpg" if extension == "jpeg" else extension
  253. new_target = f"{target}.{extension}"
  254. os.rename(target, new_target)
  255. return f"/images/{os.path.basename(new_target)}"
  256. return await asyncio.gather(*[copy_image(image) for image in images])
  257. class ImageResponse(ResponseType):
  258. def __init__(
  259. self,
  260. images: Union[str, list],
  261. alt: str,
  262. options: dict = {}
  263. ):
  264. self.images = images
  265. self.alt = alt
  266. self.options = options
  267. def __str__(self) -> str:
  268. return format_images_markdown(self.images, self.alt, self.get("preview"))
  269. def get(self, key: str):
  270. return self.options.get(key)
  271. def get_list(self) -> list[str]:
  272. return [self.images] if isinstance(self.images, str) else self.images
  273. class ImagePreview(ImageResponse):
  274. def __str__(self):
  275. return ""
  276. def to_string(self):
  277. return super().__str__()
  278. class ImageDataResponse():
  279. def __init__(
  280. self,
  281. images: Union[str, list],
  282. alt: str,
  283. ):
  284. self.images = images
  285. self.alt = alt
  286. def get_list(self) -> list[str]:
  287. return [self.images] if isinstance(self.images, str) else self.images
  288. class ImageRequest:
  289. def __init__(
  290. self,
  291. options: dict = {}
  292. ):
  293. self.options = options
  294. def get(self, key: str):
  295. return self.options.get(key)