-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageDownload.py
More file actions
188 lines (164 loc) · 5.02 KB
/
imageDownload.py
File metadata and controls
188 lines (164 loc) · 5.02 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
# João Tinti's script, https://github.com/joaotinti75/Pokemon_Classification/blob/master/google_imgs_downloader_v2.py
# adicionado apenas removedor de arquivos
import requests
import os
from PIL import Image
from bs4 import BeautifulSoup
def clearImages(directory):
img_list = []
width_list = []
height_list = []
count = 0
height = int(input('Min height to remove: '))
width = int(input('Min width to remove: '))
print('Cleaning images...\n')
for image in os.listdir(directory):
img_list.append(image)
width_list.append(Image.open(directory+image).width)
height_list.append(Image.open(directory+image).height)
print('Images found\n')
for x in width_list:
if x < width:
os.remove(directory+img_list[count])
count += 1
count = 0
print('Widht found\n')
for x in height_list:
try:
if x < height:
os.remove(directory+img_list[count])
count += 1
except FileNotFoundError:
pass
count = 0
print('height found\n')
print('All done\n')
def googleImagesDownload(directory, search, num_of_img):
search_mod = search.upper()
search_mod = search_mod.split()
# first page of google images
url1 = f'https://www.google.com/search?q={search}&hl=pt-BR&gbv=1&source=lnms&tbm=isch&sa=X&ved=2ahUKEwipwcKTxOfrAhUUDrkGHZ3kB5kQ_AUoAXoECB8QAw&sfr=gws&sei=g8xeX6WWO4KI5OUP1Ne2sAQ'
i = 0
response = requests.get(url1)
soup = BeautifulSoup(response.text, 'html.parser')
links_list = []
repeat_img = []
table = soup.find_all('table', attrs={'class': 'TxbwNb'})
for links in table:
links_list.append(links.a.get('href'))
links_list = [sites[7:sites.index('&')] for sites in links_list]
for link in links_list:
if i == num_of_img:
break
url = link
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
images = soup.find_all('img')
for image in images:
if i == num_of_img:
break
elif image.get('src') == None or '.svg' in image.get('src'):
pass
elif image.get('src') in repeat_img:
pass
else:
search_comp = [i in image.get('src').upper() for i in search_mod]
if search_comp.count(True) == len(search_comp) and 'http' in image.get('src'):
with open(f'{directory}/{search.lower()}_{i}.png', 'wb') as f:
img_response = requests.get(image.get('src'))
if img_response.status_code == 200: # sucess
if i == num_of_img:
break
else:
#print(link, '-', i)
print(image.get('src'), '-', i)
repeat_img.append(image.get('src'))
f.write(img_response.content)
i += 1
else:
continue
except:
pass
page = 20
if i < num_of_img:
while True:
links_list = []
img_list = []
urln = f'https://www.google.com/search?q={search}&hl=pt-BR&gbv=1&tbm=isch&ei=78xeX5PTGc_Z5OUPrsyA0A0&start={page}&sa=N'
response = requests.get(urln)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find_all('table', attrs={'class': 'IkMU6e'})
for links in table:
links_list.append(links.a.get('href'))
links_list = [sites[7:sites.index('&')] for sites in links_list]
for link in links_list:
if i == num_of_img:
break
url = link
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
images = soup.find_all('img')
for image in images:
if i == num_of_img:
break
elif image.get('src') == None or '.svg' in image.get('src'):
pass
elif image.get('src') in repeat_img:
pass
else:
search_comp = [i in image.get('src').upper() for i in search_mod]
if search_comp.count(True) == len(search_comp) and 'http' in image.get('src'):
with open(f'{directory}/image_{i}.png', 'wb') as f:
img_response = requests.get(image.get('src'))
if img_response.status_code == 200: # sucess
if i == num_of_img:
break
else:
#print(link, '-', i)
print(image.get('src'), '-', i)
repeat_img.append(image.get('src'))
f.write(img_response.content)
i += 1
else:
pass
except:
pass
if i < num_of_img:
page += 20
else:
break
print(f'\n{i} images were downloaded\n')
if __name__ == '__main__':
while True:
try:
directory = input('Directory: ')
if directory == '':
if 'images' in os.listdir():
pass
else:
os.mkdir('./images')
directory = './images/'
else:
if directory[-1] != '/':
directory += '/'
print(directory)
#directory = '/home/khalli/Desktop/teste/'
search = input('What images do you want to download: ')
num_of_img = int(input('How many images: '))
clear = input('Clear small images ? [Y/N]\n').upper().strip()
print('\nDownloading...Check your directory\n')
googleImagesDownload(directory, search, num_of_img)
if clear in 'SY':
clearImages(directory)
else:
pass
exit_question = input('Do you want to quit? [Y/N]').upper().strip()
if exit_question in 'SY':
print('Bye!')
break
else:
pass
except ValueError:
print('\nWrong value type!\n')