From e7709df1592ec93f81346224ed1ac8d440edcf6e Mon Sep 17 00:00:00 2001 From: Sooper Mango <168652294+SooperMango@users.noreply.github.com> Date: Mon, 27 May 2024 15:46:14 +0530 Subject: [PATCH] v1.1 --- random-img-gen.py | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/random-img-gen.py b/random-img-gen.py index 5f1440e..e312347 100644 --- a/random-img-gen.py +++ b/random-img-gen.py @@ -1,43 +1,62 @@ import requests import io import tkinter as tk -from PIL import Image,ImageTk +from tkinter import filedialog +from PIL import Image, ImageTk from ttkbootstrap import Style root = tk.Tk() -root.title('Random Image Genertor') +root.title('Random Image Generator') root.geometry('700x500') root.config(bg='white') -root.resizable(False, False) +root.resizable(True, True) style = Style(theme="sandstone") -def dispaly_image(category): - url = f'https://api.unsplash.com/photos/random?query={category}&orientation=landscape&client_id=YOUR_ACCESS_KEY' +def display_image(category): + 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 - photo = ImageTk.PhotoImage(Image.open(io.BytesIO(img_data)).resize((600, 400), resample=Image.LANCZOS)) + global photo, current_image_data + current_image_data = img_data + photo = ImageTk.PhotoImage(Image.open(io.BytesIO(img_data)).resize((700, 500), resample=Image.LANCZOS)) label.config(image=photo) label.image = photo def enable_button(*args): - generate_button.config(state='normal' if category_var.get( ) != "Choose Category" else "disabled") + generate_button.config(state='normal' if category_var.get() != "Choose Category" else "disabled") + +def save_image(): + if current_image_data: + file_path = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("All files", "*.*")]) + if file_path: + with open(file_path, 'wb') as f: + f.write(current_image_data) + +def on_right_click(event): + context_menu.post(event.x_root, event.y_root) def create_gui(): - global category_var, generate_button, label + global category_var, generate_button, label, current_image_data, context_menu + + 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', '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.grid(row=0, column=0, padx=10, pady=10, sticky='nsew') category_dropdown.config(width=15) - generate_button = tk.Button(text='Generate Image', state='disabled', command=lambda: dispaly_image(category_var.get())) + 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") label = tk.Label(root, background="white") label.grid(row=1, column=0, columnspan=2, padx=10, pady=10, sticky="nsew") - + label.bind("", on_right_click) # Bind right-click event to the label + + 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.mainloop()