Skip to content

About

Spring AI Generate Images

Resources

Stars

1 star

Watchers

0 watching

Forks

 
 

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring AI Image

A small Spring Boot application that uses Spring AI and OpenAI models for two image workflows: generate a PNG from a text prompt with /image, or upload an image to /vision and receive a detailed description of its contents.

Requirements

  • Java 21
  • An OpenAI API key with access to image generation and vision-capable chat models

Configuration

The application reads the API key from the OPENAI_API_KEY environment variable. Export it before starting the application:

export OPENAI_API_KEY=your-api-key

Run the application

Start the application with the Maven wrapper:

./mvnw spring-boot:run

The application listens on http://localhost:8080.

Generate an image

With the application running, copy and run this command in a second terminal. The response is a PNG image, so redirect it to a file:

curl --location 'http://localhost:8080/image' \
  --header 'Content-Type: application/json' \
  --data '{
    "question": "The Last JVM Engineer: a dramatic and humorous cinematic poster in which a lone Java developer stands on the roof of a server fortress during a thunderstorm, wielding a glowing golden JVM power core while an army of rogue AI-generated bugs rises from broken build pipelines. A sarcastic robot code reviewer in a hoodie leads the bugs, holding a red warning triangle like a battle standard. Behind the developer, an enormous neural network forms a protective halo around a coffee-fueled CI/CD pipeline. The developer deploys one final clean build, turning every bug into helpful unit tests. Epic scale, photorealistic detail, dramatic blue-versus-amber lighting, futuristic software engineering atmosphere, expressive characters, subtle developer humor, vertical theatrical poster composition, no readable text, no logos, no watermarks."
  }' \
  --output java-ai-poster.png

Open output/java-ai-poster.png after the request completes.

Sample result

The Last JVM Engineer - Java and AI image generation sample

Another prompt example

This prompt creates a tense, darkly humorous Java-and-AI survival scene:

curl --location 'http://localhost:8080/image' \
  --header 'Content-Type: application/json' \
  --data '{
    "question": "The Nightly Build After the Outbreak: a cinematic survival-horror scene in a deserted city reclaimed by nature, where a determined Java developer guards the last functioning data center against a massive horde of AI-generated code zombies. The undead wear torn office clothes and carry broken keyboards, empty coffee cups, and obsolete bug reports. A small service robot projects a glowing blue Java stack trace onto the fog as a warning signal, while an improvised team of developers fortifies the server room with Ethernet cables and deploys automated tests as defensive drones. Moody overcast sky, rain-slick streets, ominous atmosphere, dramatic flashlight beams, realistic practical details, tense survival storytelling, dark developer humor, no readable text, no logos, no watermarks."
  }' \
  --output java-zombie-apocalypse.png

Sample result

The Nightly Build After the Outbreak - Java and AI zombie apocalypse image

Photorealistic beach example

This example is designed for a vivid, photorealistic Copacabana beach-volleyball scene:

curl --location 'http://localhost:8080/image' \
  --header 'Content-Type: application/json' \
  --data '{
    "question": "A breathtaking photorealistic sports-editorial photograph on Copacabana Beach in Rio de Janeiro at golden hour, featuring three confident adult women in tasteful athletic beachwear playing an energetic beach-volleyball rally on a professional sand court, one athlete leaping for a spectacular overhead hit while her teammates move into position and laugh naturally. Make Copacabana unmistakable: the sweeping crescent shoreline, busy promenade, black-and-white Portuguese wave mosaic pavement, rows of palm trees, colorful beach kiosks, beachgoers, and the Christ the Redeemer statue clearly visible on the mountain ridge in the background, with Sugarloaf Mountain farther across the bay. Warm low sunlight, ocean spray and sand particles frozen in the air, authentic skin texture, detailed hair and fabric, realistic athletic motion, natural anatomy, cinematic depth, vibrant but true-to-life colors, high-end travel and sports magazine composition, 85mm lens, no readable text, no logos, no watermark."
  }' \
  --output rio-beach-ai.png

Sample result

Photorealistic Rio de Janeiro beach fashion editorial

API

POST /image

Request body:

{
  "question": "A detailed description of the image to generate"
}

The endpoint returns the generated image as image/png. The current implementation uses the gpt-image-1 model, requests a 1024x1024 image at high quality, and describes the desired photorealistic appearance in the prompt. GPT Image models return image data as Base64 by default; the application decodes it before sending the PNG to the client.

POST /vision

Upload a JPEG image as the file multipart form field to receive an AI-generated description. The vision workflow uses the gpt-4o-mini model.

curl --location 'http://localhost:8080/vision' \
  --header 'Content-Type: multipart/form-data' \
  --form 'file=@"/path/to/image.jpg"'

In Postman, replace /path/to/image.jpg with the image file selected in the form-data editor. The postman-cloud:///... file reference is specific to Postman and is not portable to a terminal outside that application.

Example response:

AssistantMessage [messageType=ASSISTANT, toolCalls=[], textContent=The image is a vibrant,
intricate illustration that features a multitude of elements arranged in a circular pattern.
The color scheme predominantly includes red and blue against a light background, creating a
striking contrast. In the center, there appears to be a stylized number "250", surrounded by
doodles representing technology, music, transportation, people, and creativity.]

Test the project

./mvnw test

Course resources

This repository contains source code examples used to support online courses about the Spring Framework.

All Spring Framework Guru Courses

Spring Framework 6

Spring Framework 5

SQL

DevOps

Web Development with Spring Framework

Connect with Spring Framework Guru

  • Spring Framework Guru Blog
  • Subscribe to Spring Framework Guru on YouTube
  • Like Spring Framework Guru on Facebook
  • Follow Spring Framework Guru on Twitter
  • Connect with John Thompson on LinkedIn

About

Spring AI Generate Images

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages