Skip to content

Instantly share code, notes, and snippets.

@24Ryou
Last active November 11, 2022 18:26
Show Gist options
  • Save 24Ryou/8589a5ef42d3edacecbe7a0c453eb0c7 to your computer and use it in GitHub Desktop.
Save 24Ryou/8589a5ef42d3edacecbe7a0c453eb0c7 to your computer and use it in GitHub Desktop.
source.unsplash get random photo
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