-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagenesgoogle.py
208 lines (162 loc) · 7.07 KB
/
imagenesgoogle.py
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
# -*- coding: utf-8 -*-
'''
Created on 27 mar. 2021
@author: Patxi Juaristi
'''
from tkinter import Tk, Frame, Text, Scrollbar, Label, Button, filedialog, \
PhotoImage, Radiobutton, IntVar, StringVar, Entry, messagebox
from tkinter.constants import END
from threading import Thread
import utils
import webbrowser
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from google_images_scraper import GoogleImagesScraper
initChrome = webdriver.Chrome(ChromeDriverManager().install())
initChrome.quit()
raiz = Tk()
icono = PhotoImage(file = utils.resource_path("imagenes-icono.png"))
raiz.iconphoto(False, icono)
raiz.title("Google Image Downloader")
miFrame=Frame(raiz, width=1200, height=700)
miFrame.pack()
rutaCarpeta=StringVar()
directorioPath=StringVar()
esHeadless=True
bgTransparente=False
formatoImg='jpg'
textResults=Text(miFrame, width=50, height=15)
textResults.grid(row=4, column=4, padx=5, pady=5)
scrollResults=Scrollbar(miFrame, command=textResults.yview)
scrollResults.grid(row=4, column=5, sticky="nsew")
textResults.config(yscrollcommand=scrollResults.set)
resultsLabel=Label(miFrame, text="Result: ", font=('Arial', 12 ))
resultsLabel.grid(row=4, column=3, padx=5, pady=5)
######33
textKeywords=Text(miFrame, width=50, height=15)
textKeywords.grid(row=4, column=1, padx=5, pady=5)
scrollKws=Scrollbar(miFrame, command=textKeywords.yview)
scrollKws.grid(row=4, column=2, sticky="nsew")
textKeywords.config(yscrollcommand=scrollKws.set)
keywordsLabel=Label(miFrame, text="Keywords: ", font=('Arial', 12 ))
keywordsLabel.grid(row=4, column=0, padx=5, pady=5)
####################################
opcionesFrame=Frame(miFrame)
opcionesFrame.grid(row=1, column=0, columnspan=5, padx=20, pady=15)
def selec():
global esHeadless
if(opcion.get()==2):
esHeadless = False
else:
esHeadless = True
opcion = IntVar()
opcion.set(1)
monitor = Label(opcionesFrame, text='Hide Chrome window', font=('Arial', 12 ))
monitor.pack(side='left')
headed = Radiobutton(opcionesFrame, text="Yes", variable=opcion, value=1, command=selec, font=('Arial', 12 )).pack(side='left')
headless = Radiobutton(opcionesFrame, text="No", variable=opcion, value=2, command=selec, font=('Arial', 12 )).pack(side='left')
#########
formatoFrame=Frame(miFrame)
formatoFrame.grid(row=2, column=0, columnspan=5, padx=20, pady=(5,10))
def selecF():
global formatoImg
global bgTransparente
bgTransparente=False
if(opcionF.get()==2):
formatoImg = 'png'
elif(opcionF.get()==3):
formatoImg = 'png'
bgTransparente=True
elif(opcionF.get()==4):
formatoImg = 'gif'
else:
formatoImg = 'jpg'
opcionF = IntVar()
opcionF.set(1)
formatoLabel = Label(formatoFrame, text='Images Format', font=('Arial', 12 ))
formatoLabel.pack(side='left')
jpgRadio = Radiobutton(formatoFrame, text="JPG", variable=opcionF, value=1, command=selecF, font=('Arial', 12 )).pack(side='left')
pngRadio = Radiobutton(formatoFrame, text="PNG", variable=opcionF, value=2, command=selecF, font=('Arial', 12 )).pack(side='left')
gifRadio = Radiobutton(formatoFrame, text="PNG (Transparent BG.)", variable=opcionF, value=3, command=selecF, font=('Arial', 12 )).pack(side='left')
gifRadio = Radiobutton(formatoFrame, text="GIF", variable=opcionF, value=4, command=selecF, font=('Arial', 12 )).pack(side='left')
####
cantidadFrame=Frame(miFrame)
cantidadFrame.grid(row=3, column=0, columnspan=5, padx=20, pady=(5,10))
cantidadLabel = Label(cantidadFrame, text='Number of Images per each Keyword:', font=('Arial', 12 ))
cantidadLabel.pack(side='left')
cantidadEntry = Entry(cantidadFrame, font=('Arial', 12 ), width=5)
cantidadEntry.pack(side='left')
def validateNumber():
try:
int(cantidadEntry.get())
return True
except:
return False
####
def split_list(a, n):
k, m = divmod(len(a), n)
return list((a[i*k+min(i, m):(i+1)*k+min(i+1, m)] for i in range(n)))
def scrapearImg(kwList, cantidad):
scraper = GoogleImagesScraper(directorioPath.get()+'/', formatoImg)
i = 1
if(scraper.checkRequisitos()):
if(scraper.initDriver(esHeadless)):
for kw in kwList:
if(scraper.scrapearImagen(kw, bgTransparente, cantidad)):
textResults.insert(float(i), kw + ' ' + str(cantidad) + '/' +str(cantidad)+ ' OK\n')
raiz.update()
else:
textResults.insert(float(i), kw + ' ERROR\n')
i+=1
else:
messagebox.showwarning(title='Chrome Driver Error', message='Error with the Chrome Driver. Probably you should need to update it')
else:
messagebox.showwarning(title='Google Block', message='Google has blocked the bot')
scraper.endDriver()
def empezarScraping():
texto = textKeywords.get("1.0", END)
kwList = texto.split('\n')
kwList = list(filter(('').__ne__, kwList))
textResults.delete(1.0,"end")
if(len(kwList) == 0):
messagebox.showwarning(title='Empty Keywords', message='You need to introduce the keywords to download the images')
return
if(directorioPath.get() != ''):
if(validateNumber() != False):
existingFiles = utils.readFolderFiles(directorioPath.get())
for fi in existingFiles:
kwExisting = fi[0:len(fi)-4].replace('-',' ')
if(kwExisting in kwList):
kwList.remove(kwExisting)
hilos = 5
divididos = split_list(kwList, hilos)
hiloCont = 1
for lista in divididos:
if(len(lista) > 0):
Thread(target = scrapearImg, args=(lista, int(cantidadEntry.get()))).start()
hiloCont += 1
else:
messagebox.showwarning(title='Incorrect Quantity', message='The number of images to download is incorrect')
else:
textResults.delete(1.0,"end")
messagebox.showwarning(title='Path Not Set', message='Set the path to storage the output files')
def establecerDirectorio():
path = filedialog.askdirectory(initialdir="/", title="Select file")
directorioPath.set(path)
rutaCarpeta.set(path)
ficheroFrame=Frame(miFrame)
ficheroFrame.grid(row=0, column=0, columnspan=5, padx=20, pady=(20,5))
nameCarpeta=Label(ficheroFrame, text="Image output folder", font=('Arial', 12 )).pack(side='left')
nameCarpetaEntry=Entry(ficheroFrame, textvariable=rutaCarpeta, width=70)
nameCarpetaEntry.config(fg="red", justify="center", font=('Arial', 12 ))
nameCarpetaEntry.pack(side='left')
botonDir=Button(ficheroFrame, text="Folder", command=establecerDirectorio, bg='white', fg='black', font=('Arial', 12 )).pack(side='left')
botonBuscar=Button(miFrame, text="Download", command=empezarScraping, bg='red', fg='white', font=('Arial', 14 ))
botonBuscar.grid(row=5, column=0, columnspan=5, pady=(15,15))
def abrirWeb(url):
webbrowser.open_new_tab(url)
link = Label(raiz, text="JuarisTech.com",font=('Helveticabold', 12), fg="blue", cursor="hand2")
link.pack(side='right')
link.bind("<Button-1>", lambda e:
abrirWeb("https://juaristech.com"))
raiz.mainloop()