Collecting free to use photos from Pexels using Python and Pexels API

In the life of any software engineer or researcher there is a moment when it is required to collect a dataset with images under free to use license.

You can easily download thousands of images from pexels.com using less than hundred lines of code. First, you need to install python wrapper for API

pip install pexels-api

Then, go to https://www.pexels.com/api/ to get a token for API. You just need to provide information on how you will use the API. After that, requested key will be generated.

API key example

Now let’s write some code. Let’s initialize api wrapper object and find photos of corgi:

After the first step, all information about available images will be stored in photos_dict. We just need to download them all:

Now we can run the script, and check if there are any images for request “corgi”.

import pandas as pd
dataframe = pd.read_json('./corgi.json', orient='index')
print(dataframe.shape)
# (21, 9) # 21 images were downloaded

You might notice that not many corgi photos (21) are available at pexels.com. However, for more popular requests search results usually contain hundreds and thousands of photos.

To code is available at GitHub

Look at these guys! Credit

Deep Learning Engineer @ Samsung AI Center, Moscow

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store