-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_gui.py
79 lines (64 loc) · 2.48 KB
/
simple_gui.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
77
78
79
# this code is just an example, I coded this in a very ugly way just to demonstrate how it can be used.
import os
import asyncio
import requests
import tkinter as tk
from tkinter import ttk, messagebox
from PyWomboAPI import WomboAPI
def download_image(url, foldername, name):
if not os.path.exists(foldername):
os.makedirs(foldername)
with open(os.path.join(foldername, name), 'wb') as file:
file.write(requests.get(url).content)
async def generate_images(style_id, prompt, image_amount):
API = WomboAPI()
API.setup()
foldername = prompt.replace(' ', '_')
async for result in API.create_image_batch(style_id, prompt, image_amount):
image_url = result.url
download_image(image_url, foldername, f"image_{result.id}.jpg")
print(f"Downloaded image: {image_url}")
messagebox.showinfo("Success", "Done generating and downloading images")
def on_generate_button_click(style_id, prompt, image_amount):
try:
image_amount = int(image_amount)
if image_amount <= 0:
raise ValueError("Really? Please generate at least one image.")
asyncio.run(generate_images(style_id, prompt, image_amount))
except ValueError as e:
messagebox.showerror("Input Error", str(e))
def fetch_styles():
API = WomboAPI()
API.setup()
styles = API.fetch_styles()
return styles
def create_gui():
root = tk.Tk()
root.title("Image Generator")
styles = fetch_styles()
prompt_label = tk.Label(root, text="Enter Prompt:")
prompt_label.pack(pady=5)
prompt_entry = tk.Entry(root, width=50)
prompt_entry.pack(pady=5)
style_label = tk.Label(root, text="Select Style:")
style_label.pack(pady=5)
style_combobox = ttk.Combobox(root, state='readonly')
style_combobox['values'] = [f"{style[0]}: {style[1]}" for style in styles]
style_combobox.pack(pady=5)
style_combobox.current(0)
amount_label = tk.Label(root, text="Number of Images:")
amount_label.pack(pady=5)
amount_entry = tk.Entry(root, width=5)
amount_entry.pack(pady=5)
generate_button = tk.Button(
root, text="Generate Images",
command=lambda: on_generate_button_click(
int(style_combobox.get().split(':')[0]), # selected style ID
prompt_entry.get().strip(), # prompt
amount_entry.get().strip() # number of images
)
)
generate_button.pack(pady=20)
root.mainloop()
if __name__ == "__main__":
create_gui()