Skip to content

Commit 04b2605

Browse files
jdomingrJuan Dominguez
andauthored
feat(genai): add image generation samples (2) (#10173)
* feat(genai): add new genai sdk image generation samples * refactor: change bucket prefix in tests * refactor: change output bucket in subject reference sample and change variable name in tests * refactor: apply some suggestions to the samples * add a more extensive header to each sample * refactor: change method comment --------- Co-authored-by: Juan Dominguez <[email protected]>
1 parent 19c4482 commit 04b2605

File tree

7 files changed

+522
-5
lines changed

7 files changed

+522
-5
lines changed

genai/snippets/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
<artifactId>google-genai</artifactId>
5454
<version>1.15.0</version>
5555
</dependency>
56+
<dependency>
57+
<groupId>com.google.cloud</groupId>
58+
<artifactId>google-cloud-storage</artifactId>
59+
</dependency>
5660
<dependency>
5761
<artifactId>junit</artifactId>
5862
<groupId>junit</groupId>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_canny_ctrl_type_with_txt_img]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.ControlReferenceConfig;
23+
import com.google.genai.types.ControlReferenceImage;
24+
import com.google.genai.types.EditImageConfig;
25+
import com.google.genai.types.EditImageResponse;
26+
import com.google.genai.types.GeneratedImage;
27+
import com.google.genai.types.Image;
28+
import java.util.List;
29+
import java.util.Optional;
30+
31+
public class ImageGenCannyCtrlTypeWithTextAndImage {
32+
33+
public static void main(String[] args) {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String modelId = "imagen-3.0-capability-001";
36+
String outputGcsUri = "gs://your-bucket/your-prefix";
37+
cannyEdgeCustomization(modelId, outputGcsUri);
38+
}
39+
40+
// Generates an image using controlled customization with a Canny Edge image and a text prompt.
41+
public static Optional<String> cannyEdgeCustomization(String modelId, String outputGcsUri) {
42+
// Client Initialization. Once created, it can be reused for multiple requests.
43+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
44+
// Create a reference image out of an existing canny edge image signal
45+
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_canny.png
46+
ControlReferenceImage controlReferenceImage =
47+
ControlReferenceImage.builder()
48+
.referenceId(1)
49+
.referenceImage(
50+
Image.builder()
51+
.gcsUri("gs://cloud-samples-data/generative-ai/image/car_canny.png")
52+
.build())
53+
.config(ControlReferenceConfig.builder().controlType("CONTROL_TYPE_CANNY").build())
54+
.build();
55+
56+
// The `[1]` in the prompt refers to the `referenceId` assigned to
57+
// the control reference image.
58+
EditImageResponse imageResponse =
59+
client.models.editImage(
60+
modelId,
61+
"a watercolor painting of a red car[1] driving on a road",
62+
List.of(controlReferenceImage),
63+
EditImageConfig.builder()
64+
.editMode("EDIT_MODE_CONTROLLED_EDITING")
65+
.numberOfImages(1)
66+
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
67+
.personGeneration("ALLOW_ADULT")
68+
.outputGcsUri(outputGcsUri)
69+
.build());
70+
71+
Image generatedImage =
72+
imageResponse
73+
.generatedImages()
74+
.flatMap(generatedImages -> generatedImages.stream().findFirst())
75+
.flatMap(GeneratedImage::image)
76+
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));
77+
78+
generatedImage.gcsUri().ifPresent(System.out::println);
79+
// Example response:
80+
// gs://your-bucket/your-prefix
81+
return generatedImage.gcsUri();
82+
}
83+
}
84+
}
85+
// [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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_raw_reference_with_txt_img]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.EditImageConfig;
23+
import com.google.genai.types.EditImageResponse;
24+
import com.google.genai.types.GeneratedImage;
25+
import com.google.genai.types.Image;
26+
import com.google.genai.types.RawReferenceImage;
27+
import java.util.List;
28+
import java.util.Optional;
29+
30+
public class ImageGenRawReferenceWithTextAndImage {
31+
32+
public static void main(String[] args) {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String modelId = "imagen-3.0-capability-001";
35+
String outputGcsUri = "gs://your-bucket/your-prefix";
36+
styleTransferCustomization(modelId, outputGcsUri);
37+
}
38+
39+
// Generates an image in a new style using style transfer customization with a raw reference image
40+
// and a text prompt.
41+
public static Optional<String> styleTransferCustomization(String modelId, String outputGcsUri) {
42+
// Client Initialization. Once created, it can be reused for multiple requests.
43+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
44+
// Create a raw reference image of teacup stored in Google Cloud Storage
45+
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/teacup-1.png
46+
RawReferenceImage rawReferenceImage =
47+
RawReferenceImage.builder()
48+
.referenceId(1)
49+
.referenceImage(
50+
Image.builder()
51+
.gcsUri("gs://cloud-samples-data/generative-ai/image/teacup-1.png")
52+
.build())
53+
.build();
54+
55+
// The `[1]` in the prompt refers to the `referenceId` assigned to the raw reference image.
56+
EditImageResponse imageResponse =
57+
client.models.editImage(
58+
modelId,
59+
"transform the subject in the image so that "
60+
+ "the teacup[1] is made entirely out of chocolate",
61+
List.of(rawReferenceImage),
62+
EditImageConfig.builder()
63+
.editMode("EDIT_MODE_DEFAULT")
64+
.numberOfImages(1)
65+
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
66+
.personGeneration("ALLOW_ADULT")
67+
.outputGcsUri(outputGcsUri)
68+
.build());
69+
70+
Image generatedImage =
71+
imageResponse
72+
.generatedImages()
73+
.flatMap(generatedImages -> generatedImages.stream().findFirst())
74+
.flatMap(GeneratedImage::image)
75+
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));
76+
77+
generatedImage.gcsUri().ifPresent(System.out::println);
78+
// Example response:
79+
// gs://your-bucket/your-prefix
80+
return generatedImage.gcsUri();
81+
}
82+
}
83+
}
84+
// [END googlegenaisdk_imggen_raw_reference_with_txt_img]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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_scribble_ctrl_type_with_txt_img]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.ControlReferenceConfig;
23+
import com.google.genai.types.ControlReferenceImage;
24+
import com.google.genai.types.EditImageConfig;
25+
import com.google.genai.types.EditImageResponse;
26+
import com.google.genai.types.GeneratedImage;
27+
import com.google.genai.types.Image;
28+
import java.util.List;
29+
import java.util.Optional;
30+
31+
public class ImageGenScribbleCtrlTypeWithTextAndImage {
32+
33+
public static void main(String[] args) {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String modelId = "imagen-3.0-capability-001";
36+
String outputGcsUri = "gs://your-bucket/your-prefix";
37+
scribbleCustomization(modelId, outputGcsUri);
38+
}
39+
40+
// Generates an image using controlled customization with a Scribble image and a text prompt.
41+
public static Optional<String> scribbleCustomization(String modelId, String outputGcsUri) {
42+
// Client Initialization. Once created, it can be reused for multiple requests.
43+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
44+
// Create a reference image out of an existing scribble image signal
45+
// using
46+
// https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_scribble.png
47+
ControlReferenceImage controlReferenceImage =
48+
ControlReferenceImage.builder()
49+
.referenceId(1)
50+
.referenceImage(
51+
Image.builder()
52+
.gcsUri("gs://cloud-samples-data/generative-ai/image/car_scribble.png")
53+
.build())
54+
.config(ControlReferenceConfig.builder().controlType("CONTROL_TYPE_SCRIBBLE").build())
55+
.build();
56+
57+
// The `[1]` in the prompt refers to the `referenceId` assigned to the
58+
// control reference image.
59+
EditImageResponse imageResponse =
60+
client.models.editImage(
61+
modelId,
62+
"an oil painting showing the side of a red car[1]",
63+
List.of(controlReferenceImage),
64+
EditImageConfig.builder()
65+
.editMode("EDIT_MODE_CONTROLLED_EDITING")
66+
.numberOfImages(1)
67+
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
68+
.personGeneration("ALLOW_ADULT")
69+
.outputGcsUri(outputGcsUri)
70+
.build());
71+
72+
Image generatedImage =
73+
imageResponse
74+
.generatedImages()
75+
.flatMap(generatedImages -> generatedImages.stream().findFirst())
76+
.flatMap(GeneratedImage::image)
77+
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));
78+
79+
generatedImage.gcsUri().ifPresent(System.out::println);
80+
// Example response:
81+
// gs://your-bucket/your-prefix
82+
83+
return generatedImage.gcsUri();
84+
}
85+
}
86+
}
87+
// [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_style_reference_with_txt_img]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.EditImageConfig;
23+
import com.google.genai.types.EditImageResponse;
24+
import com.google.genai.types.GeneratedImage;
25+
import com.google.genai.types.Image;
26+
import com.google.genai.types.StyleReferenceConfig;
27+
import com.google.genai.types.StyleReferenceImage;
28+
import java.util.List;
29+
import java.util.Optional;
30+
31+
public class ImageGenStyleReferenceWithTextAndImage {
32+
33+
public static void main(String[] args) {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String modelId = "imagen-3.0-capability-001";
36+
String outputGcsUri = "gs://your-bucket/your-prefix";
37+
styleCustomization(modelId, outputGcsUri);
38+
}
39+
40+
// Generates an image using style customization with a style reference image and text prompt.
41+
public static Optional<String> styleCustomization(String modelId, String outputGcsUri) {
42+
// Client Initialization. Once created, it can be reused for multiple requests.
43+
try (Client client = Client.builder().location("global").vertexAI(true).build()) {
44+
// Create a style reference image of a neon sign stored in Google Cloud Storage
45+
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png
46+
StyleReferenceImage styleReferenceImage =
47+
StyleReferenceImage.builder()
48+
.referenceId(1)
49+
.referenceImage(
50+
Image.builder()
51+
.gcsUri("gs://cloud-samples-data/generative-ai/image/neon.png")
52+
.build())
53+
.config(StyleReferenceConfig.builder().styleDescription("neon sign").build())
54+
.build();
55+
56+
// The `[1]` in the prompt refers to the `referenceId` assigned to the style reference image.
57+
EditImageResponse imageResponse =
58+
client.models.editImage(
59+
modelId,
60+
"generate an image of a neon sign [1] with the words: have a great day",
61+
List.of(styleReferenceImage),
62+
EditImageConfig.builder()
63+
.editMode("EDIT_MODE_DEFAULT")
64+
.numberOfImages(1)
65+
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE")
66+
.personGeneration("ALLOW_ADULT")
67+
.outputGcsUri(outputGcsUri)
68+
.build());
69+
70+
Image generatedImage =
71+
imageResponse
72+
.generatedImages()
73+
.flatMap(generatedImages -> generatedImages.stream().findFirst())
74+
.flatMap(GeneratedImage::image)
75+
.orElseThrow(() -> new IllegalStateException("No image was generated by the model."));
76+
77+
generatedImage.gcsUri().ifPresent(System.out::println);
78+
// Example response:
79+
// gs://your-bucket/your-prefix
80+
81+
return generatedImage.gcsUri();
82+
}
83+
}
84+
}
85+
// [END googlegenaisdk_imggen_style_reference_with_txt_img]

0 commit comments

Comments
 (0)