Skip to content
Starfield Labs

Tutorials

Stable Diffusion tutorial: How to use Lexica, the Stable Diffusion AI art image search engine

AI hackathon guide to Lexica: search Stable Diffusion images and call its search API, including reverse image search.

By Starfield LabsPublished Intermediate30 min

Cover card for the tutorial "Stable Diffusion tutorial: How to use Lexica, the Stable Diffusion AI art image search engine"

Welcome to this Stable Diffusion tutorial! With the help of the new open source Stable Diffusion AI model everyone can generate awesomely looking images or as everyone calls it now - AI generated art.

This tutorial will show you how to use Lexica, a new Stable Diffusion image search engine, that has millions of images generated by Stable Diffusion indexed. You can use it to just browse through images to get some inspiration or you can use their API to integrate it into your next project. The big advantage of working with Stable Diffusion based tools like Lexica is that you don't need to generate the images yourself - you can just use the ones that are already indexed. This saves lots of compute power and time.

The site was created by Sharif Shameem, who hopes it “makes Stable Diffusion prompting a bit less of a dark art and more of a science.”

How to use Lexica

You can find the site at www.lexica.art. It's very simple and easy to use. You can search for images by entering a prompt in the search bar. The prompt can be anything you want, it can be a word, a sentence or even a paragraph.

As mentioned before, you can even generate images on the site as well. Just enter a prompt and click on the generate button. You can also change the parameters of the model like width, height and seed. Furthermore you can add things you want to exclude in your generation.

How to use Stable Diffusion based Lexica with the API

Furthermore, the Lexica team has added a new API feature to get programmatic access to the images. To use Lexica's search API, create a GET request with a query parameter q as your search term. For example, to search for the term "apples", you would make a request to:

GET https://lexica.art/api/v1/search?q=apples

You can even do reverse image search. Just send the image you want and Lexica will return the most similar images. This opens up a lot of possibilities for your next project or hackathon with stable diffusion. For a reverse image search, you can pass in the URL of an image as the query:

GET https://lexica.art/api/v1/search?q=https://media.gettyimages.com/photos/crete-senesi-countryside-in-summer-tuscany-italy-picture-id1411845730

The search response will look like this:

The API will return a JSON object containing an array of 50 image search results.

{
    "images": [
        {
            // The ID of the image
            "id": "0482ee68-0368-4eca-8846-5930db866b33",
            // URL for the image's gallery
            "gallery": "https://lexica.art?q=0482ee68-0368-4eca-8846-5930db866b33",
            // Link to this image
            "src": "https://image.lexica.art/md/0482ee68-0368-4eca-8846-5930db866b33",
            // Link to an compressed & optimized version of this image
            "srcSmall": "https://image.lexica.art/sm/0482ee68-0368-4eca-8846-5930db866b33",
            // The prompt used to generate this image
            "prompt": "cute chubby blue fruits icons for mobile game ui ",
            // Image dimensions
            "width": 512,
            "height": 512,
            // Seed
            "seed": "1413536227",
            // Whether this image is a grid of multiple images
            "grid": false,
            // The model used to generate this image
            "model": "stable-diffusion",
            // Guidance scale
            "guidance": 7,
            // The ID for this image's prompt
            "promptid": "d9868972-dad8-477d-8e5a-4a0ae1e9b72b"
            // Whether this image is classified as NSFW
            "nsfw": false,
        },
    ...
    ]
}

If you want to learn more about how to write good prompts, see the tutorial on the basics of prompt engineering for Stable Diffusion.

Build it at an AI hackathon

Searching images that already exist saves compute and time, which matters in an AI hackathon with a short build window. The Lexica search API can feed a project with prompts and example images, and reverse image search opens more ideas. To find an event to try it in, see the list of upcoming AI hackathons.

Frequently asked questions

How do I search Lexica from code?

Create a GET request with a query parameter q as your search term, for example GET https://lexica.art/api/v1/search?q=apples.

Can I search by image?

Yes. Lexica supports reverse image search: pass the URL of an image as the query and it returns the most similar images.

What does the API return?

A JSON object containing an array of 50 image search results, with fields such as the image id, links, the prompt used, width and height, seed, model and guidance scale.

How this tutorial was tested

This content was carried over from an earlier tutorial and updated for Starfield Labs. It was not re-run in this build. None of the steps, code or commands was executed, and no output was checked. Running it needs: a web browser or an HTTP client and the third-party Lexica service. The tutorial does not mention an API key. None of these was used here, so versions, prices, model names and links may have changed. Check each one before you start.