Repository navigation
Expand file tree
/
Copy pathImageDownloader.py
More file actions
73 lines (63 loc) · 2.24 KB
/
Copy pathImageDownloader.py
File metadata and controls
73 lines (63 loc) · 2.24 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
from selenium.webdriver.common.by import By
import requests
import io
from PIL import Image
import time
from os import mkdir
def getImagesFromGoogle(wd, delay, maxImages):
def scrollDown():
wd.execute_script("window.scrollTo(0, document.body.scrollHeigh);")
time.sleep(delay)
imageUrls = set()
skips = 0
found = 0
while len(imageUrls) + skips < maxImages:
scrollDown()
thumbnails = wd.find_elements(By.CLASS_NAME, "Q4LuWd")
for img in thumbnails[len(imageUrls) + skips:maxImages]:
try:
img.click()
time.sleep(delay)
except Exception as e:
print("Error: ", e)
continue
images = wd.find_elements(By.CLASS_NAME, "n3VNCb")
for image in images:
if image.get_attribute('src') in imageUrls or ('http' not in image.get_attribute('src')):
maxImages += 1
skips += 1
# print("Skipped")
continue
if image.get_attribute('src') and 'http' in image.get_attribute('src'):
imageUrls.add(image.get_attribute('src'))
# print("Found image!")
found += 1
print("Found", found, "images")
print("Checked", (found+skips), "images")
return imageUrls
def downloadImage(downloadPath, url, fileName):
try:
imageContent = requests.get(url).content
imageFile = io.BytesIO(imageContent) # storing in bytes, need to onvery to img to store
image = Image.open(imageFile)
filePath = downloadPath + fileName
with open(filePath, "wb") as f:
image.save(f, "JPEG")
return 1
except Exception as e:
print("Failed: ", e)
return 0
def downloadImages(wd, delay, max, searchField="imgs"):
urls = getImagesFromGoogle(wd, delay, max)
c = 1
success = 0
pathName = "imgs/" + searchField + "/"
try:
mkdir(pathName)
except FileExistsError:
pass
for url in urls:
fileName = "img" + str(c) + ".jpg"
success += downloadImage(pathName, url, fileName)
c += 1
print("Successfully downloaded", success, "images")