Skip to content

Commit

Permalink
v1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
SooperMango committed May 31, 2024
1 parent 0dfffe7 commit 20d77b6
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions random-img-gen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
import io
import tkinter as tk
from tkinter import filedialog
from tkinter import filedialog, simpledialog
from PIL import Image, ImageTk
from ttkbootstrap import Style

Expand All @@ -13,7 +13,7 @@
style = Style(theme="sandstone")

def display_image(category):
url = f'https://api.unsplash.com/photos/random?query={category}&orientation=landscape&client_id=YOUR_ACCESS_KEY'
url = f'https://api.unsplash.com/photos/random?query={category}&orientation=landscape&client_id=kQI4yDmON9IZj74e7jI2U42kwSXanb_gQAp_mpEjUAs'
data = requests.get(url).json()
img_data = requests.get(data['urls']['regular']).content

Expand All @@ -28,10 +28,11 @@ def enable_button(*args):

def save_image():
if current_image_data:
file_path = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("All files", "*.*")])
file_path = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("PNG files", "*.png"), ("WebP files", "*.webp"), ("SVG files", "*.svg"), ("All files", "*.*")])
if file_path:
with open(file_path, 'wb') as f:
f.write(current_image_data)
img = Image.open(io.BytesIO(current_image_data))
ext = file_path.split('.')[-1]
img.save(file_path, format=ext.upper())

def on_right_click(event):
context_menu.post(event.x_root, event.y_root)
Expand All @@ -42,23 +43,24 @@ def create_gui():
current_image_data = None

category_var = tk.StringVar(value="Choose Category")
category_options = ["Choose Category", 'Food', 'Animals', 'People', 'Music', 'Art', 'Vehicles', 'Sports', 'Random']
category_options = ["Choose Category", 'Food', 'Animals', 'People', 'Music', 'Art', 'Vehicles', 'Sports', 'Architecture', 'Travel', 'Nature' ,'Random']
category_dropdown = tk.OptionMenu(root, category_var, *category_options, command=enable_button)
category_dropdown.grid(row=0, column=0, padx=10, pady=10, sticky='nsew')
category_dropdown.config(width=15)
category_dropdown.config(width=15, bg='black')

generate_button = tk.Button(text='Generate Image', state='disabled', command=lambda: display_image(category_var.get()))
generate_button.grid(row=0, column=1, padx=10, pady=10, sticky="nsew")
generate_button.config(background='black')

label = tk.Label(root, background="white")
label.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
label.bind("<Button-3>", on_right_click) # Bind right-click event to the label

label.bind("<Button-3>", on_right_click)
context_menu = tk.Menu(root, tearoff=0)
context_menu.add_command(label="Download Image", command=save_image)

root.columnconfigure([0, 1], weight=1)
root.rowconfigure(1, weight=1)
root.rowconfigure(2, weight=1)
root.mainloop()

if __name__ == '__main__':
Expand Down

0 comments on commit 20d77b6

Please sign in to comment.