Skip to content

Commit 22680c4

Browse files
jdomingrJuan Dominguez
andauthored
feat(genai): add image generation samples using Flash MM (#10169)
* feat(genai): add image generation MMFlash samples * delete some resources --------- Co-authored-by: Juan Dominguez <[email protected]>
1 parent 1af49a4 commit 22680c4

10 files changed

+578
-0
lines changed
2.07 MB
Loading
1.5 MB
Loading
2.28 MB
Loading
2.3 MB
Loading
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.imagegeneration;
18+
19+
// [START googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.Blob;
23+
import com.google.genai.types.Candidate;
24+
import com.google.genai.types.Content;
25+
import com.google.genai.types.GenerateContentConfig;
26+
import com.google.genai.types.GenerateContentResponse;
27+
import com.google.genai.types.Part;
28+
import java.awt.image.BufferedImage;
29+
import java.io.ByteArrayInputStream;
30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.nio.file.Files;
33+
import java.nio.file.Paths;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
import javax.imageio.ImageIO;
37+
38+
public class ImageGenMmFlashEditImageWithTextAndImage {
39+
40+
public static void main(String[] args) throws IOException {
41+
// TODO(developer): Replace these variables before running the sample.
42+
String modelId = "gemini-2.5-flash-image-preview";
43+
String outputFile = "resources/output/bw-example-image.png";
44+
generateContent(modelId, outputFile);
45+
}
46+
47+
// Edits an image with image and text input
48+
public static void generateContent(String modelId, String outputFile) throws IOException {
49+
// Client Initialization. Once created, it can be reused for multiple requests.
50+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
51+
52+
byte[] localImageBytes =
53+
Files.readAllBytes(Paths.get("resources/example-image-eiffel-tower.png"));
54+
55+
GenerateContentResponse response =
56+
client.models.generateContent(
57+
modelId,
58+
Content.fromParts(
59+
Part.fromBytes(localImageBytes, "image/png"),
60+
Part.fromText("Edit this image to make it look like a cartoon.")),
61+
GenerateContentConfig.builder().responseModalities("TEXT", "IMAGE").build());
62+
63+
// Get parts of the response
64+
List<Part> parts =
65+
response
66+
.candidates()
67+
.flatMap(candidates -> candidates.stream().findFirst())
68+
.flatMap(Candidate::content)
69+
.flatMap(Content::parts)
70+
.orElse(new ArrayList<>());
71+
72+
// For each part print text if present, otherwise read image data if present and
73+
// write it to the output file
74+
for (Part part : parts) {
75+
if (part.text().isPresent()) {
76+
System.out.println(part.text().get());
77+
} else if (part.inlineData().flatMap(Blob::data).isPresent()) {
78+
BufferedImage image =
79+
ImageIO.read(new ByteArrayInputStream(part.inlineData().flatMap(Blob::data).get()));
80+
ImageIO.write(image, "png", new File(outputFile));
81+
}
82+
}
83+
84+
System.out.println("Content written to: " + outputFile);
85+
86+
// Example response:
87+
// No problem! Here's the image in a cartoon style...
88+
//
89+
// Content written to: resources/output/bw-example-image.png
90+
}
91+
}
92+
}
93+
// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.imagegeneration;
18+
19+
// [START googlegenaisdk_imggen_mmflash_locale_aware_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.Blob;
23+
import com.google.genai.types.Candidate;
24+
import com.google.genai.types.Content;
25+
import com.google.genai.types.GenerateContentConfig;
26+
import com.google.genai.types.GenerateContentResponse;
27+
import com.google.genai.types.Part;
28+
import java.awt.image.BufferedImage;
29+
import java.io.ByteArrayInputStream;
30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
import javax.imageio.ImageIO;
35+
36+
public class ImageGenMmFlashLocaleAwareWithText {
37+
38+
public static void main(String[] args) throws IOException {
39+
// TODO(developer): Replace these variables before running the sample.
40+
String modelId = "gemini-2.5-flash-image-preview";
41+
String outputFile = "resources/output/example-breakfast-meal.png";
42+
generateContent(modelId, outputFile);
43+
}
44+
45+
// Generates an image with text input
46+
public static void generateContent(String modelId, String outputFile) throws IOException {
47+
// Client Initialization. Once created, it can be reused for multiple requests.
48+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
49+
50+
GenerateContentResponse response =
51+
client.models.generateContent(
52+
modelId,
53+
"Generate a photo of a breakfast meal.",
54+
GenerateContentConfig.builder().responseModalities("TEXT", "IMAGE").build());
55+
56+
// Get parts of the response
57+
List<Part> parts =
58+
response
59+
.candidates()
60+
.flatMap(candidates -> candidates.stream().findFirst())
61+
.flatMap(Candidate::content)
62+
.flatMap(Content::parts)
63+
.orElse(new ArrayList<>());
64+
65+
// For each part print text if present, otherwise read image data if present and
66+
// write it to the output file
67+
for (Part part : parts) {
68+
if (part.text().isPresent()) {
69+
System.out.println(part.text().get());
70+
} else if (part.inlineData().flatMap(Blob::data).isPresent()) {
71+
BufferedImage image =
72+
ImageIO.read(new ByteArrayInputStream(part.inlineData().flatMap(Blob::data).get()));
73+
ImageIO.write(image, "png", new File(outputFile));
74+
}
75+
}
76+
77+
System.out.println("Content written to: " + outputFile);
78+
79+
// Example response:
80+
// Here is a photo of a breakfast meal for you!
81+
//
82+
// Content written to: resources/output/example-breakfast-meal.png
83+
}
84+
}
85+
}
86+
// [END googlegenaisdk_imggen_mmflash_locale_aware_with_txt]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.imagegeneration;
18+
19+
// [START googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.Blob;
23+
import com.google.genai.types.Candidate;
24+
import com.google.genai.types.Content;
25+
import com.google.genai.types.GenerateContentConfig;
26+
import com.google.genai.types.GenerateContentResponse;
27+
import com.google.genai.types.Part;
28+
import java.awt.image.BufferedImage;
29+
import java.io.ByteArrayInputStream;
30+
import java.io.File;
31+
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
import javax.imageio.ImageIO;
35+
36+
public class ImageGenMmFlashMultipleImagesWithText {
37+
38+
public static void main(String[] args) throws IOException {
39+
// TODO(developer): Replace these variables before running the sample.
40+
String modelId = "gemini-2.5-flash-image-preview";
41+
generateContent(modelId);
42+
}
43+
44+
// Generates multiple images with text input
45+
public static List<String> generateContent(String modelId) throws IOException {
46+
// Client Initialization. Once created, it can be reused for multiple requests.
47+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
48+
49+
GenerateContentResponse response =
50+
client.models.generateContent(
51+
modelId,
52+
"Generate 3 images a cat sitting on a chair.",
53+
GenerateContentConfig.builder().responseModalities("TEXT", "IMAGE").build());
54+
55+
// Get parts of the response
56+
List<Part> parts =
57+
response
58+
.candidates()
59+
.flatMap(candidates -> candidates.stream().findFirst())
60+
.flatMap(Candidate::content)
61+
.flatMap(Content::parts)
62+
.orElse(new ArrayList<>());
63+
64+
List<String> generatedImages = new ArrayList<>();
65+
int imageCounter = 1;
66+
// For each part print text if present, otherwise read image data if present and
67+
// write it to the output file
68+
for (Part part : parts) {
69+
if (part.text().isPresent()) {
70+
System.out.println(part.text().get());
71+
} else if (part.inlineData().flatMap(Blob::data).isPresent()) {
72+
BufferedImage image =
73+
ImageIO.read(new ByteArrayInputStream(part.inlineData().flatMap(Blob::data).get()));
74+
String fileName = "resources/output/example-cats-0" + (imageCounter++) + ".png";
75+
ImageIO.write(image, "png", new File(fileName));
76+
generatedImages.add(fileName);
77+
}
78+
}
79+
80+
// Example response:
81+
// Here are three images of a cat sitting on a chair...
82+
return generatedImages;
83+
}
84+
}
85+
}
86+
// [END googlegenaisdk_imggen_mmflash_multiple_imgs_with_txt]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package genai.imagegeneration;
18+
19+
// [START googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.Blob;
23+
import com.google.genai.types.Candidate;
24+
import com.google.genai.types.Content;
25+
import com.google.genai.types.GenerateContentConfig;
26+
import com.google.genai.types.GenerateContentResponse;
27+
import com.google.genai.types.Part;
28+
import java.awt.image.BufferedImage;
29+
import java.io.BufferedWriter;
30+
import java.io.ByteArrayInputStream;
31+
import java.io.File;
32+
import java.io.FileWriter;
33+
import java.io.IOException;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
import javax.imageio.ImageIO;
37+
38+
public class ImageGenMmFlashTextAndImageWithText {
39+
40+
public static void main(String[] args) throws IOException {
41+
// TODO(developer): Replace these variables before running the sample.
42+
String modelId = "gemini-2.5-flash-image-preview";
43+
String outputFile = "resources/output/paella-recipe.md";
44+
generateContent(modelId, outputFile);
45+
}
46+
47+
// Generates text and image with text input
48+
public static void generateContent(String modelId, String outputFile) throws IOException {
49+
// Client Initialization. Once created, it can be reused for multiple requests.
50+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
51+
52+
GenerateContentResponse response =
53+
client.models.generateContent(
54+
modelId,
55+
Content.fromParts(
56+
Part.fromText("Generate an illustrated recipe for a paella."),
57+
Part.fromText(
58+
"Create images to go alongside the text as you generate the recipe.")),
59+
GenerateContentConfig.builder().responseModalities("TEXT", "IMAGE").build());
60+
61+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
62+
63+
// Get parts of the response
64+
List<Part> parts =
65+
response
66+
.candidates()
67+
.flatMap(candidates -> candidates.stream().findFirst())
68+
.flatMap(Candidate::content)
69+
.flatMap(Content::parts)
70+
.orElse(new ArrayList<>());
71+
72+
int index = 1;
73+
// For each part print text if present, otherwise read image data if present and
74+
// write it to the output file
75+
for (Part part : parts) {
76+
if (part.text().isPresent()) {
77+
writer.write(part.text().get());
78+
} else if (part.inlineData().flatMap(Blob::data).isPresent()) {
79+
BufferedImage image =
80+
ImageIO.read(new ByteArrayInputStream(part.inlineData().flatMap(Blob::data).get()));
81+
ImageIO.write(
82+
image, "png", new File("resources/output/example-image-" + index + ".png"));
83+
writer.write("![image](example-image-" + index + ".png)");
84+
}
85+
index++;
86+
}
87+
88+
System.out.println("Content written to: " + outputFile);
89+
90+
// Example response:
91+
// A markdown page for a Paella recipe(`paella-recipe.md`) has been generated.
92+
// It includes detailed steps and several images illustrating the cooking process.
93+
//
94+
// Content written to: resources/output/paella-recipe.md
95+
}
96+
}
97+
}
98+
}
99+
// [END googlegenaisdk_imggen_mmflash_txt_and_img_with_txt]

0 commit comments

Comments
 (0)