Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def decode_base64_to_image(encoding):
- content = encoding.split(";")[1]
- print(content)
- image_encoded = content.split(",")[1]
- return Image.open(BytesIO(base64.b64decode(image_encoded)))
- backupPath = os.path.join(os.getcwd(), "backups")
- if os.path.exists(backupPath) == False:
- os.mkdir(backupPath)
- txt2ImgURL = "http://your ip here/api/submit"
- extrasURL = "http://your ip here/api/submit_extras"
- def constructTxt2Img(prompt):
- return {
- "data": [
- prompt,
- "person, man, woman, robo, car, vehicle, people", #negative prompt
- "None",
- "None",
- 30,
- "Euler a",
- False,
- False,
- 1,
- 1,
- 7,
- -1,
- -1,
- 0,
- 0,
- 0,
- False,
- 384,
- 640,
- False,
- 0.7,
- 0,
- 0,
- "None",
- False,
- False,
- None,
- "",
- "Seed",
- "",
- "Nothing",
- "",
- True,
- True,
- False,
- None,
- "",
- ""
- ]
- }
- def convertBytesToPng(bytes):
- s = "data:image/png;base64," + str(base64.b64encode(bytes), "utf-8")
- return s
- def constructExtras(bytes):
- return {
- "data": [
- 0,
- 0,
- convertBytesToPng(bytes),
- None,
- 0,
- 0,
- 0,
- 3,
- 512,
- 512,
- True,
- "None",
- "None",
- 0,
- [],
- "",
- ""
- ]
- }
- def constructFileGet(file):
- return "http://your ip here/file=" + file
- backups = []
- def getRandomBackup():
- global backups
- global backupPath
- if (len(backups) == 0):
- for filename in os.listdir(backupPath):
- backups.append(os.path.join(backupPath, filename))
- random.shuffle(backups)
- if (len(backups) > 1000):
- os.remove(backups.pop(0))
- if len(backups) == 0:
- return None
- return Image.open(backups.pop(0)).convert("RGB")
- def generateNewImage(prompt):
- global backupPath
- data = constructTxt2Img(prompt)
- try:
- r = requests.post(url = txt2ImgURL, data = json.dumps(data, indent=4))
- if (r.status_code == 200):
- ret = json.loads(r.text)
- r2 = requests.get(url= constructFileGet(ret["data"][0][0]["name"]))
- if (r2.status_code == 200):
- extras = constructExtras(r2.content)
- r3 = requests.post(url = extrasURL, data = json.dumps(extras, indent = 4))
- if (r3.status_code == 200):
- r4 = requests.get(url=constructFileGet(json.loads(r3.text)["data"][0][0]["name"]))
- if r4.status_code == 200:
- i = Image.open(BytesIO(r4.content))
- i.save(os.path.join(backupPath, str(time.time()).replace(".", "")+ ".png"), "PNG")
- return i
- except:
- print("Couldn't connect to the processing server, using a backup")
- return getRandomBackup()
Add Comment
Please, Sign In to add comment