Unsplash image logo
Unsplash image logo

How to use unsplash in python

June 01

Unsplash  the internet’s source of freely-usable images.Powered by creators everywhere. According to unsplash The website claims over 207,000 contributing photographers and generates more than 17 billion photo impressions per month. So you want to use this resource in your python app 😃. Let's get started. For better experience switch to full screen mode from the more icon on the right.

How Unsplash API works

Here they have described the power of unsplash. Basically what we are doing is we are sending a http GET request to unsplash for getting images according to the needs. In their Docs they have given a huge list of requests you can make with unsplash. I have listed a few below




And more you check by yourself. I don't want to make this post long, So let's get started.

1. Registering in Unsplash.

Before creating an app just go through the API use and guidelines. Once you create your application you can find your existing apps here.

unsplash secret key and sccess key
unsplash secret key and sccess key
Keep those keys SECRET
do not share these keys because someone may use your keys then your application's access may be turned off.

2. Using unsplash with python

Get your coffee ready. Buy me a coffee if you wish by clicking the more icon > Donate icon. Let's start coding buddy.

  1. Install requests if you don't have  by running pip install requests in your cmd. see this post to know about PIP
  2. Assign your Access Key to a variable.
  3. Import requests
  4. Fetch anything from Unsplash by making a request using requests module to a Unsplash URL according to your needs. Refer Docs for different URLS

Let's see some examples 

Getting a random photo

We are only getting a random photo's data by URL to view the photo you can use pillow I will include the code examples below in this post.

The output will be a dictionary containing a photos information and its URL like below. Your output may differ because it's a random photo 🤔. NOTE : this data just contains only the data about the image not actually the image itself. To get the image  we need to make another GET request by passing the URL given in this data.

I know it's a huge data. Really huge contains all information about the image. They have given data of the photographer to image data. Also likes description 🤠 that's nice. So it helps to sort according to your needs. Let's get some  data from the JSON data. 

So now you know how to get an image's data. So what can we do with that? 🤔🤔🤔. Ans: Whatever you want but if you are using for commercial purposes you should attribute the author. In this post I will give some examples of using the image

Image manipulation using pillow

Opening the image using pillow

You need pillow library to open the image. To install pillow run pip install pillow on your Command line 

That's it image will open in your default image viewer 🤞. In my case it's Microsoft photos. Please note it takes some time to open depending upon your internet and processor speed in my case it takes 2minutes in your case it will take no longer than 10 seconds my pc is dead slow 😂 If you buy me coffee by donating I will upgrade my pc and create more posts like this often thank you 😜.  Let's come to article.

Saving an image 

 According to Unsplash download API Guidelines: When your application performs something similar to a download like we are doing below, you must send a request to the download endpoint returned under the photo i.e. the download link under link in JSON data we converted ( photo['links']['download_location']

Steps to save an image from Unsplash:

  1. Import required modules
  2. Get an image URL from API
  3. Make a download request to the API
  4. If the status code is 200 download using pillow

As I told we should make a request to Unsplash before initiating download. The API may respond with status codes 403(Forbidden) or 404(Not found) or 200(Success) and rarely some other status codes. Initiate download only when the status code is 200 as recommended by Unsplash. 

To save the image in your current folder where the python program is, just replace .show() with .save("<FILE-NAME>"") . Unfortunately Unsplash did not provide the image format separately. Yes we can get from the URL but in this code I am using .format function from pillow. Let's see the code and I will explain the working below this code snippet.

Let me explain what's happening in detail.

  1. First we are importing the required modules. i.e. Image from PIL, requests, BytesIO from io .
  2. Get a random image's data by GET method by using requests module .
  3. convert that data to JSON, so we can use it in python .
  4. Get the value of download_location  from that data. download location is a URL .
  5. Make a download request to the URL (Unsplash API) by passing the access keys. The second argument is just used to pass values to GET request. we can also pass the value by concatenating with '?client_id={ACCESS_KEY}' 
  6. If the status code from the response is 200 (success) we are following the download. 
  7. To download a image we need to make another request  GET /photos/:id/download by passing access keys. This returns a URL which we will convert to python dictionary using json and get the URL from the converted data
  8. Get the image data by making a request to the provided URL using requests
  9. Creating a filename with image caption and format
  10. Saving the image by providing image data and filename.

That's it all done check your folder where the python program is. Similarly this takes time depending on your pc and internet speed. Since it's a random photo I gave the file name as image caption itself. We can perform many operations with the image but in this post I am not doing so. If you want tell me in comments section. That's it for this post guys if you want to extend this post be feel free to tell me in the comments section. If you like this post be sure to like it. Follow me for more amazing posts. I am creating a series of amazing stuffs you can perform on python like this so be sure to follow me. Love from sriram stay safe in this COVID situation bye bye see you in the next post 🤞👋👋. It's hard to tell bye bye anyway we will see in the next post. 👋👋👋.

Today's deal from platiplus

More from the author

Unsplash image logo

How to use unsplash in python

Comments (2)

Add a Comment