-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimple_ImageScrapper.py
32 lines (23 loc) · 1.21 KB
/
Simple_ImageScrapper.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
from bing_image_downloader import downloader
from pathlib import Path
query_list = [
# Type you query here
]
downloaded_files = set()
limit_per_query = 150 # Specify the limit per query
directory = Path('PathToDataSet') # Specify the directory to save the downloaded images
# Create the directory if it doesn't exist
directory.mkdir(parents=True, exist_ok=True)
for query in query_list:
output_dir = directory / query
downloaded_images = len(list(output_dir.glob('*')))
if downloaded_images >= limit_per_query:
continue # Move on to the next query if the limit is already met
remaining_limit = limit_per_query - downloaded_images
downloader.download(query, limit=remaining_limit, output_dir=output_dir, adult_filter_off=False,
force_replace=False, timeout=60)
# Filter out non-JPEG, JPG, or PNG files
[f.unlink() for f in output_dir.glob('*') if f.suffix.lower() not in ('.jpg', '.jpeg', '.png')]
# Remove duplicates
downloaded_files |= {downloader.get_image_hash(f.read_bytes()) for f in output_dir.glob('*')}
[f.unlink() for f in output_dir.glob('*') if downloader.get_image_hash(f.read_bytes()) in downloaded_files]