Back

Top 10 APIs for searching and downloading images

2023-02-05

As technology evolves, it can be extremely helpful for developers to build automated image tools, blog post editors and artificial intelligence applications using the image search and download capabilities provided by the API. To begin with, Picture search API enables easy access to a vast library of images, they were used for everyday lookups. However, through technological advancements, image search APIs provide easy access to massive amounts of images, thus enabling developers to seamlessly integrate images into their applications without human intervention.

This not only saves time and effort but also ensures a consistent supply of high-quality images. The API also provides functionality to search for images based on keywords and various other parameters. So this makes it easier for developers to find the right images for their applications. Furthermore, by providing access to a large repository of images and advanced search functionality, these websites are a valuable resource for developers looking to build cutting-edge image-based applications.

Picture search API

Top 10 APIs for searching and downloading images

1. Unsplash API – https://unsplash.com/developers – Advantage: Large library of high-quality, royalty-free photos.

2. Pexels API – https://www.pexels.com/api/ – Advantage: This is a large library that offers a vast collection of free and high-quality photos.

3. Getty Images API – https://www.gettyimages.com/ – Advantage: Access to a vast collection of premium images.

4. Adobe Stock API – https://www.adobe.io/apis/creativecloud/stock/docs.html – Advantage: Access to a large collection of high-quality stock photos and vectors.

5. Shutterstock API – https://developers.shutterstock.com/ – Advantage: Access to a vast collection of premium images and vectors.

6. Picjumbo API – https://picjumbo.com/ – Advantage: High-quality, royalty-free photos with a simple API.

7. Flickr API – https://www.flickr.com/services/developer/api/ – Advantage: Large community of photographers and a vast collection of photos.

8. 500px API – https://developers.500px.com/ – Advantage: Access to a large collection of high-quality photos from professional photographers.

9. Google Images API – https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list – Advantage: Access to a large collection of images from the web.

10. Getty Images Open Content Program API – https://www.getty.edu/projects/open-content-program/ – Advantage: Access to a large collection of high-quality, royalty-free historical images.

Download Image API Code example:

Below is a sample Node.js code that demonstrates how to use the Unsplash API to search for office building images and then download three of those images. Other API should be similar.

First, you’ll need to install the necessary packages. You can do this by running:

bashCopy Code
npm install axios fs

Here’s the sample code:

javascriptCopy Code
const axios = require('axios');
const fs = require('fs');
const path = require('path');

// Replace with your Unsplash Access Key
const UNSPLASH_ACCESS_KEY = 'YOUR_UNSPLASH_ACCESS_KEY';

// Function to search for office building images
async function searchOfficeBuildingImages() {
    try {
        const response = await axios.get('https://api.unsplash.com/search/photos', {
            params: {
                query: 'office building',
                per_page: 3 // Number of images to fetch
            },
            headers: {
                Authorization: `Client-ID ${UNSPLASH_ACCESS_KEY}`
            }
        });

        return response.data.results;
    } catch (error) {
        console.error('Error searching for images:', error);
        throw error;
    }
}

// Function to download an image
async function downloadImage(url, filepath) {
    const writer = fs.createWriteStream(filepath);

    const response = await axios({
        url,
        method: 'GET',
        responseType: 'stream'
    });

    response.data.pipe(writer);

    return new Promise((resolve, reject) => {
        writer.on('finish', resolve);
        writer.on('error', reject);
    });
}

// Main function to search and download images
(async () => {
    try {
        const images = await searchOfficeBuildingImages();

        if (images.length === 0) {
            console.log('No images found.');
            return;
        }

        for (let i = 0; i < images.length; i++) {
            const imageUrl = images[i].urls.full;
            const filepath = path.resolve(__dirname, `office_building_${i + 1}.jpg`);
            await downloadImage(imageUrl, filepath);
            console.log(`Downloaded image ${i + 1} to ${filepath}`);
        }
    } catch (error) {
        console.error('Error in main function:', error);
    }
})();

Steps to Run the Code:

  1. Get Unsplash Access Key: Sign up on Unsplash and create a new application to get your access key.
  2. Replace Placeholder: Replace 'YOUR_UNSPLASH_ACCESS_KEY' with your actual Unsplash access key.
  3. Run the Script: Save the script to a file, e.g., download_images.js, and run it using Node.js:
    bashCopy Code
    node download_images.js
    

This script will search for images related to “office building” on Unsplash and download the first three images to the current directory.

Conclusion

To sum up, the choice of API may vary based on the quality and licensing of the images needed for a blog post. Sflow.io is a great content creation and blog development and automation tool. Sign up for free and test out the content development features.