-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-image-downloader.py
More file actions
348 lines (265 loc) · 11.9 KB
/
Copy pathgoogle-image-downloader.py
File metadata and controls
348 lines (265 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import math
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tktooltip import ToolTip
from idlelib.tooltip import Hovertip
import sys
from threading import Thread
import sv_ttk
import requests
from selenium import webdriver
import os
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
import subprocess
import base64
import json
import pathlib
from subprocess import CREATE_NO_WINDOW # This flag will only be available in windows
# Get current directory
curr_dir = str(pathlib.Path().resolve())
# Data to be written
dictionary = { "output_folder": "" }
# Serializing json
json_object = json.dumps(dictionary, indent=4)
# Opening JSON file
try:
with open(curr_dir + '/' + 'settings.json', 'r') as readfile:
settings_file = json.load(readfile)
except:
with open(curr_dir + "/" + "settings.json", "w") as outfile:
outfile.write(json_object)
with open(curr_dir + '/' + 'settings.json', 'r') as readfile:
settings_file = json.load(readfile)
# get 'output_folder' value from settings.json
folder_path = settings_file["output_folder"]
# Redirect stdout to text widget
def redirect_stdout_to_text(widget):
class StdoutRedirector:
def __init__(self, text_widget):
self.text_widget = text_widget
def write(self, message):
self.text_widget.configure(state=tk.NORMAL)
self.text_widget.insert(tk.END, message)
self.text_widget.configure(state=tk.DISABLED)
self.text_widget.see(tk.END)
def flush(self):
pass
sys.stdout = StdoutRedirector(widget)
# Crear la ventana principal
root = tk.Tk()
ancho_pantalla = root.winfo_screenwidth()
alto_pantalla = root.winfo_screenheight()
posicion_x = (ancho_pantalla - 300) // 2
posicion_y = (alto_pantalla - 850) // 2
# # Main window configuration
root.wm_geometry(f"+{posicion_x}+{posicion_y}") # Posición X + Posición Y
root.title("Google Img Downloader")
root.resizable(False, False)
main_frame = ttk.Frame(root)
main_frame.pack(pady=(15,10))
def image_scraping(input_search, count, is_transp, is_hq, folder):
if not input_search:
print("'Search input' is invalid.")
elif count < 1 or count > 99:
print("'Image count' is invalid.")
elif not folder:
print("'Output folder' is invalid'.")
else:
print('--------------')
transparent = ''
if is_transp:
transparent = '&tbs=ic:trans'
high_quality = ''
if is_hq:
high_quality = '&tbs=isz:l'
# Crea la subcarpeta utilizando el input de búsqueda dentro de la carpeta especificada
subfolder_path = os.path.join(folder, input_search)
# Verifica si la subcarpeta existe, y si no, la crea
if not os.path.isdir(subfolder_path):
try:
os.makedirs(subfolder_path)
except:
print("'Output folder' is invalid'.")
# Open output dir
if os.path.isdir(subfolder_path):
if os.name == 'nt': # Windows
os.startfile(subfolder_path)
elif os.name == 'posix': # macOS o Linux
subprocess.Popen(['open', subfolder_path])
print("Output folder opened...")
else:
print("Can't open output folder.")
print("Downloading...")
def download_image(url, num, isb64):
# write image to file
if not isb64:
reponse = requests.get(url)
if reponse.status_code==200:
with open(os.path.join(subfolder_path, input_search+"_"+str(num)+".png"), 'wb') as file:
file.write(reponse.content)
if isb64:
image_data = base64.b64decode(url.split(",")[1])
with open(os.path.join(subfolder_path, input_search+"_"+str(num)+".png"), 'wb') as file:
file.write(image_data)
chrome_options = webdriver.ChromeOptions()
# Add your options as needed
options = [
# Define window size here
# "--window-size=1200,1200",
"--ignore-certificate-errors",
"--allow-running-insecure-content",
"--headless",
"--disable-gpu",
"--disable-extensions",
"--no-sandbox",
"--disable-dev-shm-usage",
'--disable-infobars',
'--incognito',
'--disable-popup-blocking'
]
for option in options:
chrome_options.add_argument(option)
chromedriver_path = os.path.abspath("chromedriver.exe")
service = Service(chromedriver_path)
# hide selenium driver console window
service.creation_flags = CREATE_NO_WINDOW
driver = webdriver.Chrome(options=chrome_options, service=service)
search_URL = f"https://www.google.com/search?tbm=isch&q={input_search}{transparent}{high_quality}"
driver.get(search_URL)
#Scrolling all the way up
driver.execute_script("window.scrollTo(0, 0);")
for i in range(1, count+1):
xPath = """//*[@id="islrg"]/div[1]/div[%s]"""%(i)
previewImageXPath = """//*[@id="islrg"]/div[1]/div[%s]/a[1]/div[1]/img"""%(i)
previewImageElement = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.XPATH, previewImageXPath))
previewImageURL = previewImageElement.get_attribute("src")
# print("preview URL", previewImageURL)
driver.find_element(By.XPATH, xPath).click()
time.sleep(1)
#It's all about the wait
timeStarted = time.time()
while True:
try:
# png, jpg //*[@id="Sva75c"]/div[2]/div[2]/div[2]/div[2]/c-wiz/div/div/div/div[3]/div[1]/a/img[1]
# //*[@id="Sva75c"]/div[2]/div[2]/div[2]/div[2]/c-wiz/div/div/div/div[2]/div/a/img[1]
imageElement = WebDriverWait(driver, 3).until(lambda x: x.find_element(By.XPATH, """//*[@id="Sva75c"]/div[2]/div[2]/div[2]/div[2]/c-wiz/div/div/div/div[3]/div[1]/a/img[1]"""))
except:
# GIF
imageElement = WebDriverWait(driver, 3).until(lambda x: x.find_element(By.XPATH, """//*[@id="Sva75c"]/div[2]/div[2]/div[2]/div[2]/c-wiz/div/div/div/div[2]/div/a/img[1]"""))
imageURL= imageElement.get_attribute('src')
if imageURL != previewImageURL:
# print("actual URL", imageURL)
b64_decode = False
break
else:
# making a timeout if the full res image can't be loaded
currentTime = time.time()
if currentTime - timeStarted > 10:
#print("Timeout! Will download a lower resolution image...")
b64_decode = True
break
#Downloading image
try:
download_image(imageURL, i, b64_decode)
print("Downloaded image %s/%s." % (i, count))
print('OK')
except:
print("Couldn't download image %s ..."%(i))
print('--------------')
print("Finished downloading all images!")
driver.quit()
# High quality
# https://www.google.com/search?q=query&tbm=isch&tbs=isz:l
def run_thread(input_search,count,transparent, is_hq, folder):
# Crear un hilo para ejecutar el bucle de impresión de números
thread = Thread(target=image_scraping, args=(input_search, count, transparent, is_hq, folder))
# Iniciar el hilo
thread.start()
# ingresar la cadena de busqueda
label_1 = ttk.Label(main_frame, text="Search input", width=12)
label_1.grid(column=0, row=0, pady=(5,10), padx=(10,0))
entry_1 = ttk.Entry(main_frame, width=24, font=("TkDefaultFont", 10))
entry_1.grid(column=1, row=0,columnspan=3, pady=(5,10), padx=(0,10), sticky="w")
entry_1.focus_set()
# cantidad de imagenes
label_2 = ttk.Label(main_frame, text="Image count", width=12)
label_2.grid(column=0, row=1, pady=(15,10), padx=(10,0))
entry_2 = ttk.Entry(main_frame, width=24, font=("TkDefaultFont", 10))
entry_2.insert(0, "5") # Insertar el valor predeterminado "5"
entry_2.grid(column=1, row=1,columnspan=3, pady=(15,10), sticky="w")
# Crear un widget Checkbutton para activar/desactivar el modo transparente
label_3 = ttk.Label(main_frame, text="Transparent", width=12)
label_3.grid(column=0, row=2, pady=(11,10), padx=(10,0))
TranspVar = tk.IntVar()
check_transp = ttk.Checkbutton(main_frame, variable=TranspVar)
check_transp.grid(column=1, row=2, pady=(11,10), sticky="w")
# Crear un widget Checkbutton para activar/desactivar el modo transparente
label_4 = ttk.Label(main_frame, text="High quality", width=12)
label_4.grid(column=0, row=3, pady=(11,10), padx=(10,0))
HQVar = tk.IntVar()
check_hd = ttk.Checkbutton(main_frame, variable=HQVar)
check_hd.grid(column=1, row=3, pady=(11,10), sticky="w")
label_5 = ttk.Label(main_frame, text="Output folder", width=12)
label_5.grid(column=0, row=4, pady=(11,15), padx=(10,0))
# Variable para almacenar el texto actualizado
folder_output = tk.StringVar()
folder_output.set('../' + folder_path.split("/")[-1]) # Valor inicial de la variable
def change_folder():
global folder_path
folder_path = filedialog.askdirectory() # Abre el diálogo de selección de carpeta
if folder_path:
folder_output.set('../' + folder_path.split("/")[-1] )
print('--------------')
print("Output folder: ", folder_path)
# Write path into json file
settings_file["output_folder"] = folder_path
with open(curr_dir + '/' + 'settings.json', 'w') as writefile:
json.dump(settings_file, writefile, indent=4)
def folder_name():
global folder_path
return folder_path
# Crear un Button para seleccionar una carpeta
browse_folder = ttk.Button(main_frame, text="Browse", command=change_folder)
browse_folder.grid(column=1, row=4, pady=(11,15), sticky="w")
label_6 = ttk.Label(main_frame, textvariable=folder_output, width=12)
label_6.grid(column=2, row=4, pady=(11,15), padx=(0,0), sticky="w")
label_6_tip = ToolTip(label_6, msg=folder_name, follow=True, delay=0.5)
# label_6_tip = Hovertip(label_6,str(folder_path),500)
separator = ttk.Separator(main_frame, orient="horizontal")
separator.grid(column=0, row=5, columnspan=3, sticky="ew", pady=10, padx=(10,0))
def run_thread_with_params():
# Obtener los valores de los widgets o variables necesarios
input_search = entry_1.get()
try:
count = int(entry_2.get())
except:
count = 0
transparent = bool(TranspVar.get())
high_quality = bool(HQVar.get())
global folder_path
# Llamar a la función run_thread con los parámetros
run_thread(input_search, count, transparent, high_quality, folder_path)
download_btn = ttk.Button(main_frame, text="Download Images", command=run_thread_with_params, style="Accent.TButton", padding=(10, 5))
download_btn.grid(column=0, row=6, columnspan=3, pady=10)
# Función para simular la pulsación del botón al presionar Enter
def on_enter_key(event):
download_btn.invoke()
# Vincular el evento Enter/Return al botón
root.bind_all("<Return>", on_enter_key)
# Crear un widget Text para mostrar la salida de la consola
text_widget = tk.Text(main_frame, width=40, height=7, font=("Consolas", 10), wrap=tk.WORD, blockcursor=True, state=tk.DISABLED, highlightcolor="gray25", fg="gray75")
text_widget.configure(state=tk.NORMAL)
text_widget.insert(tk.END, 'Download status... \n')
text_widget.configure(state=tk.DISABLED)
text_widget.see(tk.END)
text_widget.grid(column=0, row=7, columnspan=3, sticky="ew", pady=(10,5), padx=(10,0))
# Redireccionar la salida estándar a la widget Text
redirect_stdout_to_text(text_widget)
# Set dark theme
sv_ttk.set_theme("dark")
# Iniciar el bucle principal de Tkinter
root.mainloop()