image_io.py 1014 B

123456789101112131415161718192021222324252627282930313233
  1. import json
  2. import os
  3. from utils.logger import logger
  4. def save_image(img, message_id, img_key):
  5. # 确保目录存在,如果不存在则创建
  6. output_directory = f"data/input_images/{message_id}/{img_key}"
  7. os.makedirs(output_directory, exist_ok=True)
  8. # 生成图片文件路径
  9. image_path = os.path.join(output_directory, f"{img_key}.jpg")
  10. # 保存图片
  11. with open(image_path, 'wb') as f:
  12. f.write(img)
  13. def get_img_key_from_input_images(message_id):
  14. input_directory = f"data/input_images/{message_id}"
  15. img_key = []
  16. for file in os.listdir(input_directory):
  17. if not file.endswith(".json"):
  18. img_key.append(file)
  19. return img_key
  20. def create_image_path(message_id, img_key):
  21. # 确保目录存在,如果不存在则创建
  22. output_directory = f"data/input_images/{message_id}/{img_key}"
  23. os.makedirs(output_directory, exist_ok=True)
  24. def get_img_file(img_path):
  25. with open(img_path, 'rb') as f:
  26. img = f.read()
  27. return img