image.py 7.4 KB

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