DevTurtle logo DevTurtle

Spring AI – Generate images with Open AI

guide/ Spring AI guide

The advent of artificial intelligence has revolutionized multiple sectors, from industrial automation to medicine. But one of the most fascinating applications is certainly the ability to generate realistic images through machine learning algorithms. In this chapter of our guide on Spring AI, we will see how to generate images with OpenAI in java applications.

OpenAI’s DALL-E model

DALL-E is an AI model, specifically an image generator, created by OpenAI. What makes it extraordinary is its ability to generate realistic images from short textual descriptions. The name of the model itself is a play on words that evokes the famous surrealist painter Salvador Dalí.

The OpenAI APIs make two models available to users:

  • DALL-E 3: it is the most recent model and currently only allows the generation of new high definition images. Supported formats are:  1024×1024, 1024×1792 or 1792×1024 pixels.
  • DALL-E 2: This is the older model but has fewer limitations than DALL-E 3. In addition to generating new images, it can also be used to edit existing images.

Image generation tutorial

The tutorial will start as always from our basic project created with Spring AI for which you can download the source code from our GitHub repository:

git icon
Git RepositoryDonload source code

The code of our program is very simple and is entirely enclosed within the following RestController:

Java
package com.example.aidemo.controller;

import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OpenAiImageController {
	
	private final ImageModel imageClient;
	
	@Autowired
    public OpenAiImageController(ImageModel imageClient) {
        this.imageClient = imageClient;
    } 
	
	@GetMapping("/ai/image")
    public String generate(@RequestParam(value = "message") String message) {
        
		ImageResponse response = imageClient.call(
		        new ImagePrompt(message,
		        OpenAiImageOptions.builder()
		                .withQuality("hd")
		                .withN(1)
		                .withHeight(1024)
		                .withWidth(1024)
		                .build()));
		
		return response.getResult().getOutput().getUrl();
    }

}

Although the meaning is quite understandable at first glance, below are some comments on the properties that can be passed as input to the model:

  • withQuality: if set to “hd” it produces better quality images (the value is only compatible with the “dall-e-3” model). If not specified, this parameter will be set to “standard”.
  • withN: indicates the number of alternatives to generate. If set with a number other than 1, it creates multiple images based on the prompt provided in input.
  • withWidth and withHeight: indicate the size of the image (width and height).

Unless otherwise specified, the model used by default is DALL-E 3. To change the model you can set the following property:

spring.ai.openai.image.options.model=<model>

The result can be obtained in base64 or, as in our example, you can retrieve the URL of the generated image.

To test the program we can invoke the endpoint we have defined:

http://localhost:8080/ai/image?message=Can you create an image of a cat sitting on a chair?

The result we obtained with our test is the following:

Spring AI image generation example
Spring AI image generation example

I hope this tutorial has been useful for understanding how to generate images with Spring AI and as usual I conclude by inviting you to read the other articles in the guide as well.