Get Images & Videos for free from keywords using Pixabay API

Anvi Kohli
2 min readMar 25, 2024

--

Visual content plays a pivotal role in captivating audiences and conveying messages effectively. However, acquiring them often comes with a hefty price tag. Fortunately, Pixabay offers a treasure of over 4.4 million free assets, ranging from stunning photos to captivating videos, all accessible through their API. In this article, we’ll explore how you can leverage the Pixabay API to source images and videos using keywords in python language.

We will be using the pixabay library in python to execute today's task. So, lets dive in!

First step — Get your Pixabay API. Go to Pixabay Developer API. Tap on Get Started and generate your own API key.

After signing up, Incase you can’t find your API key. Go to this link Pixabay API Documentation and find it in front of key in the parameters section.

Now, let’s get to the coding part

Install the pixabay library

pip install pixabay

then, write down these functions which queries and download the media according to your keyword.

For images —

def get_images_from_keywords_api(keyword):
# search for the keyword
media= px.query(keyword)

# get len of hits len(space)
print("{} hits".format(len(media)))
name = keyword.split(" ")
name = "_".join(name)

# downalod first image
media[0].download("image_{}.jpg".format(name), "largeImage")

Images supports 3 formats — webformat, largeImage and preview. Whereas, video support 4 parameters — large, medium, small and tiny

For Videos -

def get_video_from_keywords_api(keyword):
# search for keyword
media= px.queryVideo(keyword)

# get len of hits len(space)
print("{} hits".format(len(media)))
name = keyword.split(" ")
name = "_".join(name)

# downalod first video
media[0].download("video_{}.mp4".format(name), "large")

You can change the number inside the [ ] brackets in the last line to download the other images and videos next in line from the query.

Now, time to run them the functions

#get image
get_images_from_keywords_api("music")

#get video
get_video_from_keywords_api("music")

And thats it. The image and video will automatically get downloaded and saved in your root directory.

image fetched using Pixabay API. Keyword — “music”

Thank You so much for reading till the end, Until next time :)

Check out my GitHub for more related codes — anvichip (Anvi Kohli) (github.com)

--

--