utils.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import io
  2. import os
  3. import re
  4. from urllib.parse import urlparse
  5. from PIL import Image
  6. from common.log import logger
  7. def fsize(file):
  8. if isinstance(file, io.BytesIO):
  9. return file.getbuffer().nbytes
  10. elif isinstance(file, str):
  11. return os.path.getsize(file)
  12. elif hasattr(file, "seek") and hasattr(file, "tell"):
  13. pos = file.tell()
  14. file.seek(0, os.SEEK_END)
  15. size = file.tell()
  16. file.seek(pos)
  17. return size
  18. else:
  19. raise TypeError("Unsupported type")
  20. def compress_imgfile(file, max_size):
  21. if fsize(file) <= max_size:
  22. return file
  23. file.seek(0)
  24. img = Image.open(file)
  25. rgb_image = img.convert("RGB")
  26. quality = 95
  27. while True:
  28. out_buf = io.BytesIO()
  29. rgb_image.save(out_buf, "JPEG", quality=quality)
  30. if fsize(out_buf) <= max_size:
  31. return out_buf
  32. quality -= 5
  33. def split_string_by_utf8_length(string, max_length, max_split=0):
  34. encoded = string.encode("utf-8")
  35. start, end = 0, 0
  36. result = []
  37. while end < len(encoded):
  38. if max_split > 0 and len(result) >= max_split:
  39. result.append(encoded[start:].decode("utf-8"))
  40. break
  41. end = min(start + max_length, len(encoded))
  42. # 如果当前字节不是 UTF-8 编码的开始字节,则向前查找直到找到开始字节为止
  43. while end < len(encoded) and (encoded[end] & 0b11000000) == 0b10000000:
  44. end -= 1
  45. result.append(encoded[start:end].decode("utf-8"))
  46. start = end
  47. return result
  48. def get_path_suffix(path):
  49. path = urlparse(path).path
  50. return os.path.splitext(path)[-1].lstrip('.')
  51. def convert_webp_to_png(webp_image):
  52. from PIL import Image
  53. try:
  54. webp_image.seek(0)
  55. img = Image.open(webp_image).convert("RGBA")
  56. png_image = io.BytesIO()
  57. img.save(png_image, format="PNG")
  58. png_image.seek(0)
  59. return png_image
  60. except Exception as e:
  61. logger.error(f"Failed to convert WEBP to PNG: {e}")
  62. raise
  63. def remove_markdown_symbol(text: str):
  64. # 移除markdown格式,目前先移除**
  65. if not text:
  66. return text
  67. return re.sub(r'\*\*(.*?)\*\*', r'\1', text)