This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
forked from RajKKapadia/Google-Image-Downloader-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_image_per_query.py
154 lines (133 loc) · 4.39 KB
/
single_image_per_query.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
import time
import base64
from io import BytesIO
import re
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
import requests
from PIL import Image
cwd = os.getcwd()
IMAGE_FOLDER = 'download'
os.makedirs(
name=f'{cwd}/{IMAGE_FOLDER}',
exist_ok=True
)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(
service=service
)
SLEEP_TIME = 0.2
def download_google_images(search_query: str, number_of_images: int) -> str:
'''Download google images with this function\n
Takes -> search_query, number_of_images\n
Returns -> None
'''
url = 'https://images.google.com/'
driver.get(
url=url
)
box = driver.find_element(
by=By.XPATH,
value="//input[contains(@class,'gLFyf gsfi')]"
)
box.send_keys(search_query)
box.send_keys(Keys.ENTER)
time.sleep(SLEEP_TIME)
img_results = driver.find_elements(
by=By.XPATH,
value="//img[contains(@class,'rg_i Q4LuWd')]"
)
total_images = len(img_results)
count = 0
for img_result in img_results:
try:
WebDriverWait(
driver,
15
).until(
EC.element_to_be_clickable(
img_result
)
)
img_result.click()
time.sleep(SLEEP_TIME)
actual_imgs = driver.find_elements(
by=By.XPATH,
value="//img[contains(@class,'n3VNCb')]"
)
src = ''
for actual_img in actual_imgs:
if 'https://encrypted' in actual_img.get_attribute('src'):
pass
elif 'http' in actual_img.get_attribute('src'):
src += actual_img.get_attribute('src')
break
else:
pass
for actual_img in actual_imgs:
if src == '' and 'base' in actual_img.get_attribute('src'):
src += actual_img.get_attribute('src')
if 'https://' in src:
image_name = search_query.replace('/', ' ')
image_name = re.sub(pattern=" ", repl="_", string=image_name)
file_path = f'{IMAGE_FOLDER}/{count}_{image_name}.jpeg'
try:
result = requests.get(src, allow_redirects=True, timeout=10)
open(file_path, 'wb').write(result.content)
img = Image.open(file_path)
img = img.convert('RGB')
img.save(file_path, 'JPEG')
print('Image saved from https.')
return file_path
except:
print('Bad image.')
try:
os.unlink(file_path)
except:
pass
count -= 1
else:
img_data = src.split(',')
image_name = search_query.replace('/', ' ')
image_name = re.sub(pattern=" ", repl="_", string=image_name)
file_path = f'{IMAGE_FOLDER}/{count}_{image_name}.jpeg'
try:
img = Image.open(BytesIO(base64.b64decode(img_data[1])))
img = img.convert('RGB')
img.save(file_path, 'JPEG')
print('Image saved from Base64.')
return file_path
except:
print('Bad image.')
count -= 1
except ElementClickInterceptedException as e:
count -= 1
print(e)
print('Image is not clickable.')
driver.quit()
count += 1
if count >= total_images:
print('No more images to download.')
break
if count == number_of_images:
break
tags = [
'Elon Musk',
'Tim Cook',
'Sundar Pichai',
'Steve Jobs'
]
for tag in tags:
file_path = download_google_images(
tag,
1
)
print(file_path)
driver.quit()