-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from MadMaximusJB/image_gen
Added image generation functionality
- Loading branch information
Showing
4 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ google-cloud-texttospeech | |
SpeechRecognition==3.8.1 | ||
keyboard==0.13.5 | ||
setuptools | ||
requests | ||
requests | ||
Pillow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from openai import OpenAI | ||
import requests | ||
from PIL import Image | ||
from io import BytesIO | ||
import os | ||
import platform | ||
import subprocess | ||
from config import OPENAI_API_KEY | ||
|
||
def generate_image(prompt): | ||
try: | ||
# Set the API key | ||
client = OpenAI(api_key=OPENAI_API_KEY) | ||
|
||
response = client.images.generate( | ||
prompt=prompt, | ||
n=1, | ||
size="1024x1024", | ||
model="dall-e-3", | ||
) | ||
|
||
# Access the URL from the response object | ||
image_url = response.data[0].url | ||
image_response = requests.get(image_url) | ||
image = Image.open(BytesIO(image_response.content)) | ||
return image | ||
except Exception as e: | ||
print(f"Error generating image: {e}") | ||
return None | ||
|
||
def save_and_open_image(image, file_path='generated_image.png'): | ||
try: | ||
image.save(file_path) | ||
if platform.system() == "Windows": | ||
os.startfile(file_path) | ||
elif platform.system() == "Linux": | ||
subprocess.call(["xdg-open", file_path]) | ||
else: | ||
print(f"Opening images is not supported on this platform: {platform.system()}") | ||
except Exception as e: | ||
print(f"Error saving or opening image: {e}") |