Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import zipfile
|
5 |
+
import shutil
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
|
9 |
+
def download_images(image_links, image_names, directory):
|
10 |
+
# Links und Namen aufteilen und leere Zeilen entfernen
|
11 |
+
links = [link.strip() for link in image_links.strip().split('\n') if link.strip()]
|
12 |
+
names = [name.strip() for name in image_names.strip().split('\n') if name.strip()]
|
13 |
+
|
14 |
+
# Ergebnistext und Bildvorschauen
|
15 |
+
result_text = ""
|
16 |
+
previews = []
|
17 |
+
|
18 |
+
# Verzeichnis erstellen, falls nicht vorhanden
|
19 |
+
if directory.strip():
|
20 |
+
directory = directory.strip()
|
21 |
+
os.makedirs(directory, exist_ok=True)
|
22 |
+
else:
|
23 |
+
directory = "images"
|
24 |
+
os.makedirs(directory, exist_ok=True)
|
25 |
+
|
26 |
+
# Prüfen, ob Links und Namen verfügbar sind
|
27 |
+
if not links:
|
28 |
+
return "Keine Links angegeben.", [], None
|
29 |
+
|
30 |
+
# Falls weniger Namen als Links, fülle mit generischen Namen auf
|
31 |
+
while len(names) < len(links):
|
32 |
+
names.append(f"image_{len(names) + 1}.jpg")
|
33 |
+
|
34 |
+
# Bilder herunterladen
|
35 |
+
successful = 0
|
36 |
+
failed = 0
|
37 |
+
downloaded_files = []
|
38 |
+
|
39 |
+
for i, (link, name) in enumerate(zip(links, names)):
|
40 |
+
try:
|
41 |
+
# Prüfen, ob der Name eine Dateiendung hat
|
42 |
+
if not any(name.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.webp']):
|
43 |
+
name = name + '.jpg' # Standard-Endung hinzufügen
|
44 |
+
|
45 |
+
filepath = os.path.join(directory, name)
|
46 |
+
|
47 |
+
# Bild herunterladen
|
48 |
+
response = requests.get(link, timeout=15)
|
49 |
+
if response.status_code == 200:
|
50 |
+
with open(filepath, 'wb') as f:
|
51 |
+
f.write(response.content)
|
52 |
+
successful += 1
|
53 |
+
downloaded_files.append(filepath)
|
54 |
+
|
55 |
+
# Bildvorschau für die Anzeige erstellen (alle Bilder)
|
56 |
+
try:
|
57 |
+
img = Image.open(io.BytesIO(response.content))
|
58 |
+
previews.append(img)
|
59 |
+
except Exception as e:
|
60 |
+
# Wenn Vorschau fehlschlägt, überspringen
|
61 |
+
pass
|
62 |
+
else:
|
63 |
+
failed += 1
|
64 |
+
result_text += f"Fehler bei {link} -> {name}: HTTP Status {response.status_code}\n"
|
65 |
+
except Exception as e:
|
66 |
+
failed += 1
|
67 |
+
result_text += f"Fehler bei {link} -> {name}: {str(e)}\n"
|
68 |
+
|
69 |
+
# ZIP-Datei erstellen, wenn mindestens ein Bild erfolgreich heruntergeladen wurde
|
70 |
+
zip_path = None
|
71 |
+
if successful > 0:
|
72 |
+
try:
|
73 |
+
zip_filename = f"{directory}.zip"
|
74 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
75 |
+
for file in downloaded_files:
|
76 |
+
zipf.write(file)
|
77 |
+
result_text += f"\nAlle Bilder wurden in {zip_filename} gepackt.\n"
|
78 |
+
zip_path = zip_filename
|
79 |
+
except Exception as e:
|
80 |
+
result_text += f"\nFehler beim Erstellen der ZIP-Datei: {str(e)}\n"
|
81 |
+
|
82 |
+
# Zusammenfassung
|
83 |
+
summary = f"Download abgeschlossen! {successful} Bilder erfolgreich, {failed} fehlgeschlagen.\n"
|
84 |
+
result_text = summary + result_text
|
85 |
+
|
86 |
+
return result_text, previews, zip_path
|
87 |
+
|
88 |
+
# Gradio-Interface erstellen
|
89 |
+
with gr.Blocks(title="Bild-Download-App") as app:
|
90 |
+
gr.Markdown("# Bild-Downloader")
|
91 |
+
gr.Markdown("Geben Sie Bildlinks und gewünschte Dateinamen ein (jeweils ein Link/Name pro Zeile)")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column(scale=1):
|
95 |
+
image_links = gr.Textbox(
|
96 |
+
placeholder="https://example.com/image1.jpg\nhttps://example.com/image2.jpg",
|
97 |
+
label="Bildlinks (ein Link pro Zeile)",
|
98 |
+
lines=10
|
99 |
+
)
|
100 |
+
image_names = gr.Textbox(
|
101 |
+
placeholder="strand.jpg\nberg.jpg",
|
102 |
+
label="Dateinamen (ein Name pro Zeile)",
|
103 |
+
lines=10
|
104 |
+
)
|
105 |
+
directory = gr.Textbox(
|
106 |
+
placeholder="images",
|
107 |
+
label="Verzeichnis (leer lassen für 'images')",
|
108 |
+
value="images"
|
109 |
+
)
|
110 |
+
download_button = gr.Button("Bilder herunterladen")
|
111 |
+
|
112 |
+
with gr.Column(scale=1):
|
113 |
+
result_output = gr.Textbox(label="Ergebnis", lines=10)
|
114 |
+
zip_download = gr.File(label="ZIP-Datei zum Download")
|
115 |
+
gr.Markdown("### Bildvorschau")
|
116 |
+
image_gallery = gr.Gallery(label="Heruntergeladene Bilder")
|
117 |
+
|
118 |
+
# Beispiele hinzufügen
|
119 |
+
gr.Examples(
|
120 |
+
[
|
121 |
+
[
|
122 |
+
"https://images.unsplash.com/photo-1599982890963-3aabd60064d2\nhttps://images.unsplash.com/photo-1530122037265-a5f1f91d3b99",
|
123 |
+
"munich-skyline.jpg\nswiss-alps.jpg",
|
124 |
+
"destinations"
|
125 |
+
],
|
126 |
+
[
|
127 |
+
"https://images.unsplash.com/photo-1436491865332-7a61a109cc05\nhttps://images.unsplash.com/photo-1519494026892-80bbd2d6fd0d",
|
128 |
+
"airport-transfer.jpg\nmedical-tourism.jpg",
|
129 |
+
"services"
|
130 |
+
]
|
131 |
+
],
|
132 |
+
[image_links, image_names, directory],
|
133 |
+
"Beispiele"
|
134 |
+
)
|
135 |
+
|
136 |
+
download_button.click(
|
137 |
+
fn=download_images,
|
138 |
+
inputs=[image_links, image_names, directory],
|
139 |
+
outputs=[result_output, image_gallery, zip_download]
|
140 |
+
)
|
141 |
+
|
142 |
+
# App starten
|
143 |
+
if __name__ == "__main__":
|
144 |
+
app.launch()
|