-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_stable_diffusion_image.py
50 lines (43 loc) · 1.41 KB
/
generate_stable_diffusion_image.py
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
49
50
from PIL import Image
import requests
import os
import json
from dotenv import load_dotenv
load_dotenv()
STABLE_DIFFUSION_API_KEY = os.getenv("STABLE_DIFFUSION_API_KEY")
def generate_stable_diffusion_image(prompt,lab: str):
url = "https://stablediffusionapi.com/api/v3/text2img"
payload = json.dumps({
"key": STABLE_DIFFUSION_API_KEY, # you should set this to your actual Stable Diffusion API key
"prompt": prompt,
"negative_prompt": None,
"width": "1080",
"height": "1080",
"samples": "1",
"num_inference_steps": "20",
"seed": None,
"guidance_scale": 7.5,
"safety_checker": "yes",
"multi_lingual": "no",
"panorama": "no",
"self_attention": "no",
"upscale": "no",
"embeddings_model": "embeddings_model_id",
"webhook": None,
"track_id": None
})
headers = {
'Content-Type': 'application/json'
}
try:
response = requests.post(url, headers=headers, data=payload)
response.raise_for_status()
image_url = response.json()['output'][0]
# Save the image data to a file
with open(f'output/bg_{lab}.png', 'wb') as f:
f.write(requests.get(image_url).content)
# Open the file using `Image.open`
image = Image.open(f'output/bg_{lab}.png')
image.save(f'output/bg_{lab}.png')
except Exception as e:
print("Error generating image:", e)