"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

November 20, 2023

Image Generation DALLE 3 vs Stable Diffusion

Prompt - A picturesque countryside in autumn splendor, with rolling hills adorned in warm fall colors, a quaint farmhouse sitting amidst harvested fields, Latest 2023 Tesla Car nestled under a canopy of russet leaves.

DALLE 3




Stable Diffusion

#EXAMPLE #1
#!pip install openai
from openai import OpenAI
import os
os.environ["OPENAI_API_KEY"] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
client = OpenAI()
response = client.images.generate(
model="dall-e-3",
prompt="A picturesque countryside in autumn splendor, with rolling hills adorned in warm fall colors, a quaint farmhouse sitting amidst harvested fields, Latest 2023 Tesla Car nestled under a canopy of russet leaves.",
size="1024x1024",
quality="standard",
n=1,
)
image_url = response.data[0].url
image_url
#EXAMPLE #2
import base64
import os
import requests
#https://platform.stability.ai/docs/api-reference#tag/v1engines/operation/listEngines
#Stability-AI Stable Diffusion XL v1.0
#engine_id = "stable-diffusion-xl-1024-v1-0"
#Stability-AI Stable Diffusion v2.1
engine_id = "stable-diffusion-512-v2-1"
api_host = os.getenv('API_HOST', 'https://api.stability.ai')
api_key = os.getenv("STABILITY_API_KEY", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
if api_key is None:
raise Exception("Missing Stability API key.")
response = requests.post(
f"{api_host}/v1/generation/{engine_id}/text-to-image",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"Bearer {api_key}"
},
json={
"text_prompts": [
{
"text": "A picturesque countryside in autumn splendor, with rolling hills adorned in warm fall colors, a quaint farmhouse sitting amidst harvested fields, Latest 2023 Tesla Car nestled under a canopy of russet leaves."
}
],
"cfg_scale": 7,
"height": 1024,
"width": 1024,
"samples": 1,
"steps": 50,
"style_preset":"photographic",
"clip_guidance_preset":"FAST_BLUE",
},
)
if response.status_code != 200:
raise Exception("Non-200 response: " + str(response.text))
data = response.json()
for i, image in enumerate(data["artifacts"]):
with open(f"v1_txt2img_{i}.png", "wb") as f:
f.write(base64.b64decode(image["base64"]))
from PIL import Image
import matplotlib.pyplot as plt
# Assuming you know the filename or you get it from the upload step
filename = 'v1_txt2img_0.png'
# Open the image file using PIL
image = Image.open(filename)
# Display the image using Matplotlib
plt.imshow(image)
plt.axis('off') # Hide the axis to make it look cleaner
plt.show()
view raw texttoimage.py hosted with ❤ by GitHub

Keep Exploring!!!

No comments: