-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
59 lines (42 loc) · 1.67 KB
/
runner.py
File metadata and controls
59 lines (42 loc) · 1.67 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
from bs4 import BeautifulSoup as bs
from requests import get
from skimage import io
import cv2, os
search_keyword = input("Enter search title: ").title()
# get current working directory
path = os.getcwd()
# create a folder for output files
base_output_folder_name = "downloads"
base_output_folder = os.path.join(path, base_output_folder_name)
if not os.path.isdir(base_output_folder): os.mkdir(base_output_folder)
# create a subfolder for the search query
output_folder = os.path.join(base_output_folder, search_keyword)
if not os.path.isdir(output_folder): os.mkdir(output_folder)
print(f"Output images will be saved inside {base_output_folder}/{search_keyword} directory" )
# get the html of the search page
url = f"https://www.google.com/search?q={search_keyword}&tbm=isch"
page = get(url)
soup = bs(page.text, "html.parser")
# get all the images
images = soup.find_all("img")[1:]
# download the images
for index, image in enumerate(images):
link = image.get("src") # get the link of the image
img = io.imread(link)
# dimensions of the image (high, width, channels)
hight, width, _ = img.shape # _ is a dummy variable
# resize the image thrice of its original size
img = cv2.resize(img, (width*3, hight*3))
# convert to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow(search_keyword, img)
# image name and path
image_path = os.path.join(output_folder, f"{search_keyword}{index}.jpg")
# save the image
print(f"Saving to {image_path}")
cv2.imwrite(image_path, img)
# wait for 100ms and if q is pressed then break
if cv2.waitKey(100) == ord("q"):
break
# destroy all the windows
cv2.destroyAllWindows()