Skip to content

Commit 168da49

Browse files
Merge pull request #2799 from smty2018/imgdownloader
Image Downloader GUI
2 parents 285440d + c4570c0 commit 168da49

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Image Downloader GUI/ReadMe.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Description:
2+
A graphical user interface (GUI) application that enables users to download images from the internet.
3+
Users can input an image URL, provide a desired image name, and specify a folder path for saving the downloaded image.
4+
5+
Requirements:
6+
pip install tkinter
7+
pip install requests
8+
9+
10+
Output:
11+
12+
![Screenshot 2023-08-10 051845](https://github.com/smty2018/Amazing-Python-Scripts/assets/74114936/28b76433-5c66-4781-a447-88cc7fd9e712)

Image Downloader GUI/dog.jpg

160 KB
Loading

Image Downloader GUI/main.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import tkinter as tk
2+
from tkinter import messagebox
3+
import os
4+
import requests
5+
6+
def get_ext(url: str) -> str | None:
7+
exts = [".png", ".jpeg", ".jpg"]
8+
for ext in exts:
9+
if ext in url:
10+
return ext
11+
return None
12+
13+
def download_img():
14+
u = url_ent.get()
15+
n = name_ent.get()
16+
f = folder_ent.get()
17+
18+
if not u or not n or not f:
19+
messagebox.showerror("Error", "Please fill in all fields.")
20+
return
21+
22+
ext = get_ext(u)
23+
if not ext:
24+
messagebox.showerror("Error", "Invalid image URL.")
25+
return
26+
27+
img_path = os.path.join(f, f"{n}{ext}")
28+
29+
if os.path.isfile(img_path):
30+
messagebox.showerror("Error", "A file with the same name already exists.")
31+
return
32+
33+
try:
34+
img_content = requests.get(u).content
35+
with open(img_path, "wb") as handler:
36+
handler.write(img_content)
37+
messagebox.showinfo("Success", f"Image downloaded to:\n{img_path}")
38+
except Exception as e:
39+
messagebox.showerror("Error", f"An error occurred: {e}")
40+
41+
root = tk.Tk()
42+
root.title("Image Downloader")
43+
root.configure(bg="#1e1e1e")
44+
45+
ul = tk.Label(root, text="Image URL:", bg="#1e1e1e", fg="white")
46+
ul.pack(pady=5)
47+
48+
url_ent = tk.Entry(root, width=50)
49+
url_ent.pack(pady=5)
50+
51+
nl = tk.Label(root, text="Image Name:", bg="#1e1e1e", fg="white")
52+
nl.pack(pady=5)
53+
54+
name_ent = tk.Entry(root, width=50)
55+
name_ent.pack(pady=5)
56+
57+
fl = tk.Label(root, text="Folder Path:", bg="#1e1e1e", fg="white")
58+
fl.pack(pady=5)
59+
60+
folder_ent = tk.Entry(root, width=50)
61+
folder_ent.pack(pady=5)
62+
63+
dl_btn = tk.Button(root, text="Download", command=download_img, bg="#303030", fg="white")
64+
dl_btn.pack(pady=10)
65+
66+
root.mainloop()

0 commit comments

Comments
 (0)