-
Notifications
You must be signed in to change notification settings - Fork 1
/
maingenimg.py
76 lines (60 loc) · 2.53 KB
/
maingenimg.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import customtkinter as ctk
import tkinter
import os
import openai
from PIL import Image, ImageTk
import requests, io
from image_widgets import *
def generate():
openai.api_key = "your key"
user_prompt = prompt_entry.get("0.0", tkinter.END)
user_prompt += "in style: " + style_dropdown.get()
response = openai.Image.create(
prompt=user_prompt,
size="512x512"
)
image_urls = []
for i in range(len(response['data'])):
image_urls.append(response['data'][i]['url'])
print(image_urls)
images = []
for url in image_urls:
response = requests.get(url)
image = Image.open(io.BytesIO(response.content))
photo_image = ImageTk.PhotoImage(image)
images.append(photo_image)
def update_image(index=0):
canvas.image = images[index]
canvas.create_image(0, 0, anchor="nw", image=images[index])
index = (index + 1) % len(images)
canvas.after(3000, update_image, index)
def save_image(self, name, file, path):
if images:
# Save the last displayed image to the Downloads folder
last_image = images[-1]
filename = "generated_image.png"
last_image.image.save(os.path.join(os.path.expanduser("~"), "Downloads", filename))
print(f"Image saved as {filename} in Downloads folder.")
update_image()
root = ctk.CTk()
root.title("Romera Editor")
ctk.set_appearance_mode("dark")
input_frame = ctk.CTkFrame(root)
input_frame.pack(side="left", expand=True, padx=20, pady=20)
prompt_label = ctk.CTkLabel(input_frame, text="Prompt")
prompt_label.grid(row=0,column=0, padx=10, pady=10)
prompt_entry = ctk.CTkTextbox(input_frame, height=10)
prompt_entry.grid(row=0,column=1, padx=10, pady=10)
style_label = ctk.CTkLabel(input_frame, text="Style")
style_label.grid(row=1,column=0, padx=10, pady=10)
style_dropdown = ctk.CTkComboBox(input_frame, values=["Realistic", "Cartoon", "3D Illustration", "Flat Art"])
style_dropdown.grid(row=1, column=1, padx=10, pady=10)
# generate button
generate_button = ctk.CTkButton(input_frame, text="Generate", command=generate)
generate_button.grid(row=3, column=0, columnspan=2, sticky="news", padx=10, pady=10)
# Create "Save" button and link it to the save_image function
save_button = ctk.CTkButton(input_frame, text="Save", command=save_image)
save_button.grid(row=4, column=0, columnspan=2, sticky="news", padx=10, pady=10)
canvas = tkinter.Canvas(root, width=512, height=512)
canvas.pack(side="left")
root.mainloop()