Skip to content

Image Downloader GUI #2799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Image Downloader GUI/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Description:
A graphical user interface (GUI) application that enables users to download images from the internet.
Users can input an image URL, provide a desired image name, and specify a folder path for saving the downloaded image.

Requirements:
pip install tkinter
pip install requests


Output:

![Screenshot 2023-08-10 051845](https://github.com/smty2018/Amazing-Python-Scripts/assets/74114936/28b76433-5c66-4781-a447-88cc7fd9e712)
Binary file added Image Downloader GUI/dog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions Image Downloader GUI/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import tkinter as tk
from tkinter import messagebox
import os
import requests

def get_ext(url: str) -> str | None:
exts = [".png", ".jpeg", ".jpg"]
for ext in exts:
if ext in url:
return ext
return None

def download_img():
u = url_ent.get()
n = name_ent.get()
f = folder_ent.get()

if not u or not n or not f:
messagebox.showerror("Error", "Please fill in all fields.")
return

ext = get_ext(u)
if not ext:
messagebox.showerror("Error", "Invalid image URL.")
return

img_path = os.path.join(f, f"{n}{ext}")

if os.path.isfile(img_path):
messagebox.showerror("Error", "A file with the same name already exists.")
return

try:
img_content = requests.get(u).content
with open(img_path, "wb") as handler:
handler.write(img_content)
messagebox.showinfo("Success", f"Image downloaded to:\n{img_path}")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")

root = tk.Tk()
root.title("Image Downloader")
root.configure(bg="#1e1e1e")

ul = tk.Label(root, text="Image URL:", bg="#1e1e1e", fg="white")
ul.pack(pady=5)

url_ent = tk.Entry(root, width=50)
url_ent.pack(pady=5)

nl = tk.Label(root, text="Image Name:", bg="#1e1e1e", fg="white")
nl.pack(pady=5)

name_ent = tk.Entry(root, width=50)
name_ent.pack(pady=5)

fl = tk.Label(root, text="Folder Path:", bg="#1e1e1e", fg="white")
fl.pack(pady=5)

folder_ent = tk.Entry(root, width=50)
folder_ent.pack(pady=5)

dl_btn = tk.Button(root, text="Download", command=download_img, bg="#303030", fg="white")
dl_btn.pack(pady=10)

root.mainloop()