Skip to content

Instantly share code, notes, and snippets.

@fsndzomga
Created February 4, 2024 15:08
Show Gist options
  • Save fsndzomga/41468e811e4f9217ae4b01aa68057217 to your computer and use it in GitHub Desktop.
Save fsndzomga/41468e811e4f9217ae4b01aa68057217 to your computer and use it in GitHub Desktop.
import base64
import os
import requests
from dotenv import load_dotenv
from PIL import Image, ImageOps
# Load environment variables
load_dotenv()
api_key = os.getenv("STABILITY_API_KEY")
response = requests.post(
"https://api.stability.ai/v1/generation/stable-diffusion-v1-6/image-to-image",
headers={
"Accept": "application/json",
"Authorization": f"Bearer {api_key}"
},
files={
"init_image": open("init_image.jpg", "rb")
},
data={
"init_image_mode": "IMAGE_STRENGTH",
"image_strength": 0.57,
"steps": 50,
"seed": 0,
"cfg_scale": 8,
"samples": 1,
"style_preset": "enhance",
"text_prompts[0][text]": 'Improve the interior design of the image provided. In the style of charley harper, post-minimalist structures, vancouver school, intricate ceiling designs, midcentury modern, angular constructions, contemporary candy-coated --ar 3:2',
"text_prompts[0][weight]": 1,
}
)
if response.status_code != 200:
raise Exception("Non-200 response: " + str(response.text))
data = response.json()
# make sure the out directory exists
if not os.path.exists("./out"):
os.makedirs("./out")
for i, image in enumerate(data["artifacts"]):
with open(f'./out/img2img_{image["seed"]}.jpg', "wb") as f:
f.write(base64.b64decode(image["base64"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment