Stop Using Lorem Picsum
Unsplash has 4M+ high-resolution photos from professional photographers, all free for commercial use. Their API gives you programmatic access with 50 requests per hour on the free tier.
Setup
Register at unsplash.com/developers. Create an app to get your access key.
import requests
ACCESS_KEY = "your_unsplash_access_key"
BASE = "https://api.unsplash.com"
HEADERS = {"Authorization": f"Client-ID {ACCESS_KEY}"}
Search Photos
def search_photos(query, count=5):
r = requests.get(f"{BASE}/search/photos", headers=HEADERS,
params={"query": query, "per_page": count})
return [{"id": p["id"],
"url": p["urls"]["regular"],
"thumb": p["urls"]["thumb"],
"author": p["user"]["name"],
"description": p.get("alt_description", "")}
for p in r.json()["results"]]
photos = search_photos("mountain landscape")
for p in photos:
print(f"{p[author]}: {p[description]}")
print(f" URL: {p[url]}")
Get Random Photo
def random_photo(query=None):
params = {}
if query:
params["query"] = query
r = requests.get(f"{BASE}/photos/random", headers=HEADERS, params=params)
p = r.json()
return {"url": p["urls"]["regular"], "author": p["user"]["name"]}
photo = random_photo("coding")
print(f"Photo by {photo[author]}: {photo[url]}")
Download and Save
def download_photo(photo_id, filename):
# First trigger download (required by Unsplash guidelines)
r = requests.get(f"{BASE}/photos/{photo_id}/download", headers=HEADERS)
download_url = r.json()["url"]
# Then download the actual image
img = requests.get(download_url)
with open(filename, "wb") as f:
f.write(img.content)
print(f"Saved to {filename}")
Real Use Cases
- Blog cover images — auto-select relevant photos for articles
- Placeholder content — fill mockups with real professional photos
- Social media — generate post images programmatically
- Desktop wallpaper — daily wallpaper changer script
- Email templates — dynamic header images based on topic
Unsplash vs Alternatives
| Service | Free Photos | API? | Commercial Use? |
|---|---|---|---|
| Unsplash | 4M+ | Yes (50/hr) | Yes |
| Pexels | 3M+ | Yes (200/hr) | Yes |
| Pixabay | 2.7M+ | Yes (100/hr) | Yes |
| Lorem Picsum | Random only | Yes | Yes |
Important: Attribution
Unsplash license does not require attribution, but it is appreciated. Their API guidelines require triggering the download endpoint when a user downloads a photo.
More from me: 10 Dev Tools I Use Daily | 77 Scrapers on a Schedule | 150+ Free APIs
Top comments (0)