Last active
June 29, 2021 16:28
-
-
Save mlagunas/ea5a156bbdc5c4ca485dc12042f4a3be to your computer and use it in GitHub Desktop.
Downloads hdris from the website https://hdrihaven.com/hdris/. It allows to select a category and resolution.
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
# FIRST do: pip install beautifulsoup4 tqdm requests fake-useragent urllib3 | |
# | |
import requests | |
import os | |
from sys import argv | |
from bs4 import BeautifulSoup | |
from urllib.parse import urlparse | |
from urllib.request import urlretrieve | |
from urllib.request import URLopener | |
from fake_useragent import UserAgent | |
from tqdm import tqdm | |
def main(): | |
# arguments | |
resolution = '1k' | |
category = 'all' | |
path_to_save = 'CHANGE THIS TO YOUR PATH' | |
# start the downloading process | |
tqdm.write("Resolution: %s" % resolution) | |
tqdm.write("Category: %s" % category) | |
ua = UserAgent() | |
opener = URLopener() | |
opener.addheader('User-Agent', ua.chrome) | |
url = 'https://hdrihaven.com/hdris/' | |
url_category = url + '?c=' + category | |
r = requests.get(url_category, | |
allow_redirects=True, | |
headers={'User-Agent': ua.chrome}) | |
soup = BeautifulSoup(r.text, 'html.parser') | |
save_to_exr = os.path.join(path_to_save, 'hdri_' + resolution, 'hdr') | |
save_to_jpg = os.path.join(path_to_save, 'hdri_' + resolution, 'ldr') | |
tqdm.write('Saving files in %s' % save_to_exr) | |
os.makedirs(save_to_exr, exist_ok=True) | |
os.makedirs(save_to_jpg, exist_ok=True) | |
hdris = soup.select('#item-grid a') | |
for hdri in tqdm(hdris): | |
thumbnail = hdri.select('.thumbnail')[0]['data-src'] | |
href = urlparse(hdri['href']) | |
filename = href.query[2:] + '_' + resolution | |
thumbnail_url = 'https://hdrihaven.com' + thumbnail | |
dl_basename = 'https://hdrihaven.com/files/hdris/' + filename | |
dl_basepath = save_to_exr + '/' + filename | |
has_errors_hdr = False | |
has_errors_exr = False | |
# attempt .hdr download | |
try: | |
dl_url = dl_basename + '.hdr' | |
dl_path = dl_basepath + '.hdr' | |
if not os.path.isfile(dl_path): | |
opener.retrieve(dl_url, dl_path) | |
except Exception as e: | |
has_errors_hdr = True | |
tqdm.write('[HDR] Error reading .hdr trying with .exr') | |
# attempt .exr download | |
if has_errors_hdr: | |
try: | |
dl_url = dl_basename + '.exr' | |
dl_path = dl_basepath + '.exr' | |
if not os.path.isfile(dl_path): | |
asd = opener.retrieve(dl_url, dl_path) | |
except Exception as e: | |
has_errors_exr = True | |
tqdm.write('[EXR] Error with .exr format') | |
# store the thumbnail of the correspoding hdri | |
if not has_errors_exr or not has_errors_hdr: | |
opener.retrieve(thumbnail_url, save_to_jpg + '/' + filename + '.jpg') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment