__init__.py 8.6 KB

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