tendent_router.py 982 B

1234567891011121314151617181920212223242526272829303132
  1. from fastapi import APIRouter
  2. import random
  3. from api.skylight import create_image,overlay_images
  4. import os
  5. tendentRouter = APIRouter()
  6. @tendentRouter.post("/tendentest")
  7. def read_root(client_message :str = "心想事成"):
  8. pic_num = random.randint(0,19)
  9. # 產生
  10. text_img_output_path = f"{os.path.split(os.path.abspath('main.py'))[0]}/static/tendents/vertical_chinese_text_{pic_num}.png"
  11. create_image(client_message, text_img_output_path)
  12. print(f"save to {text_img_output_path}")
  13. output_path = f"{os.path.split(os.path.abspath('main.py'))[0]}/static/tendents/combined_image_{pic_num}.png"
  14. try:
  15. os.remove(output_path)
  16. except OSError as e:
  17. print('Delete Problem: ', e)
  18. # 執行疊加
  19. overlay_images(f"{os.path.split(os.path.abspath('main.py'))[0]}/static/tendentest.png", text_img_output_path, output_path)
  20. url = f"static/tendents/combined_image_{pic_num}.webp"
  21. return {"state":"success","url": url }