1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import requests from base64 import b64encode, b64decode import json
url = "https://qwq.bvipc.com/sdapi/v1/txt2img"
account_id = "xxxxx" account_password = "xxxxxxxxxxxx"
credentials = f"{account_id}:{account_password}" encoded_credentials = b64encode(credentials.encode("utf-8")).decode("utf-8") auth_header = { "Authorization": f"Basic {encoded_credentials}", "Content-Type": "application/json", }
data = { "prompt": "draw a lovely girly", "steps": 10, "width": 512, "height": 512, "cfg_scale": 7.5, "seed": -1, "sampler_index": "Euler a", }
response = requests.post(url, data=json.dumps(data), headers=auth_header)
if response.status_code == 200: response_data = response.json() images = response_data.get('images', []) for i, image_base64 in enumerate(images, start=1): image_data = b64decode(image_base64) image_filename = f"image_{i}.png" with open(image_filename, "wb") as image_file: image_file.write(image_data) print(f"图像已保存为:{image_filename}") else: print("Error:", response.status_code, response.text)
|