craiyon.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. import requests
  3. import base64
  4. import sys
  5. from PIL import Image
  6. import os
  7. import subprocess
  8. VIEWER = ["mpv", "--keep-open"]
  9. FORMAT = "jpeg"
  10. def main():
  11. headers = {
  12. "authority": "backend.craiyon.com",
  13. "accept": "application/json",
  14. "accept-language": "en-GB,en-US;q=0.9,en;q=0.8",
  15. "content-type": "application/json",
  16. "origin": "https://www.craiyon.com",
  17. "referer": "https://www.craiyon.com/",
  18. "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15",
  19. }
  20. prompt = ' '.join(sys.argv[1:])
  21. if prompt == "":
  22. print(f"Need to give a prompt of what you want to see!\nExample: {sys.argv[0]} A flying pig.")
  23. sys.exit(1)
  24. print("This will take some time...")
  25. data = requests.post("https://backend.craiyon.com/generate", headers=headers, json={"prompt": prompt}).json()
  26. for i, image in enumerate(data["images"]):
  27. image = image.replace('"','').replace("\\n","\n")
  28. decodedata = base64.b64decode(image)
  29. with open(f"{i+1}.jpeg","wb") as f:
  30. f.write(decodedata)
  31. grid = Image.new('RGB', size=(3*256, 3*256))
  32. grid.paste(Image.open("1.jpeg"), (0, 0))
  33. grid.paste(Image.open("2.jpeg"), (256, 0))
  34. grid.paste(Image.open("3.jpeg"), (512, 0))
  35. grid.paste(Image.open("4.jpeg"), (0, 256))
  36. grid.paste(Image.open("5.jpeg"), (256, 256))
  37. grid.paste(Image.open("6.jpeg"), (512, 256))
  38. grid.paste(Image.open("7.jpeg"), (0, 512))
  39. grid.paste(Image.open("8.jpeg"), (256, 512))
  40. grid.paste(Image.open("9.jpeg"), (512, 512))
  41. filename = f'{prompt.lower().replace(" ","_")}.{FORMAT}'
  42. grid.save(filename, FORMAT.upper())
  43. OPEN = []
  44. OPEN += VIEWER
  45. OPEN.append(filename)
  46. subprocess.Popen(OPEN)
  47. if __name__ == "__main__":
  48. main()