generated from CU-ESIIL/Innovation-Summit-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7591bbf
commit 5a217d1
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import requests | ||
from pyinaturalist import get_taxa, get_observations | ||
|
||
|
||
# Function to create a directory to save images | ||
def create_directory(directory): | ||
if not os.path.exists(directory): | ||
os.makedirs(directory) | ||
|
||
|
||
# Function to download and save images | ||
def download_image(image_url, save_path): | ||
response = requests.get(image_url, stream=True) | ||
if response.status_code == 200: | ||
with open(save_path, 'wb') as file: | ||
for chunk in response.iter_content(1024): | ||
file.write(chunk) | ||
|
||
|
||
# Function to scrape images of a given taxon (species) | ||
def scrape_images(taxon_name): | ||
# Search for the taxon by name | ||
taxa = get_taxa(q=taxon_name) | ||
if not taxa['results']: | ||
print(f"No taxa found for '{taxon_name}'") | ||
return | ||
|
||
# Get the taxon ID | ||
taxon_id = taxa['results'][0]['id'] | ||
|
||
# Get observations for the taxon | ||
observations = get_observations(taxon_id=taxon_id) | ||
|
||
# Directory to save images | ||
directory = f'inat_images_{taxon_name.replace(" ", "_")}' | ||
create_directory(directory) | ||
|
||
# Loop through each observation and download the images | ||
for obs in observations['results']: | ||
for photo in obs['photos']: | ||
image_url = photo['url'].replace('square', 'large') | ||
file_extension = image_url.split('.')[-1] | ||
filename = f"{obs['id']}_{photo['id']}.{file_extension}" | ||
save_path = os.path.join(directory, filename) | ||
|
||
# Download and save the image | ||
download_image(image_url, save_path) | ||
print(f"Downloaded {filename}") | ||
|
||
|
||
# Run the scraper | ||
taxon_name = 'Calypte anna' | ||
scrape_images(taxon_name) |