Last active
November 11, 2022 18:26
-
-
Save 24Ryou/8589a5ef42d3edacecbe7a0c453eb0c7 to your computer and use it in GitHub Desktop.
source.unsplash get random photo
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
import requests | |
from pathlib import Path | |
# Download an image off unsplash without the api using python | |
def downloadimages(search_term, resolution, amount): # Define the function to download images | |
print(f"https://source.unsplash.com/random/{resolution}/?"+str(search_term)+", allow_redirects=True") # State the URL | |
path = Path('YourLocalPathForSaveFile') | |
# Loop for chosen amount of times | |
for x in range(int(amount)): | |
# Download the photo(s) | |
response = requests.get(f"https://source.unsplash.com/random/{resolution}/?"+str(search_term)+", allow_redirects=True") | |
# State the filename | |
print("Saving to:" + str(path) + "\\" + str(search_term) + str(x+1) + ".png") | |
# Write image file | |
open(str(path) + "\\" + str(search_term) + str(x+1) + ".png", 'wb').write(response.content) | |
def get(search_term): | |
resolution = '900x600' | |
amount = 1 | |
# search_term = input('Enter the search term: ') | |
downloadimages(search_term, resolution, amount) # Call the Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment