-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(genai): add image generation samples (2) #10173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdomingr
wants to merge
7
commits into
GoogleCloudPlatform:main
Choose a base branch
from
jdomingr:genai-sdk-imggen-samples-p2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e815694
feat(genai): add new genai sdk image generation samples
abeeb6b
refactor: change bucket prefix in tests
73a8f2b
refactor: change output bucket in subject reference sample and change…
4d7fdf5
resolve merge conflicts
854f847
refactor: apply some suggestions to the samples
af3dd39
add a more extensive header to each sample
434de93
refactor: change method comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...i/snippets/src/main/java/genai/imagegeneration/ImageGenCannyCtrlTypeWithTextAndImage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package genai.imagegeneration; | ||
|
||
// [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img] | ||
|
||
import com.google.genai.Client; | ||
import com.google.genai.types.ControlReferenceConfig; | ||
import com.google.genai.types.ControlReferenceImage; | ||
import com.google.genai.types.EditImageConfig; | ||
import com.google.genai.types.EditImageResponse; | ||
import com.google.genai.types.GeneratedImage; | ||
import com.google.genai.types.Image; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class ImageGenCannyCtrlTypeWithTextAndImage { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String modelId = "imagen-3.0-capability-001"; | ||
String outputGcsUri = "gs://your-bucket/your-prefix"; | ||
generateImageWithCannyCtrl(modelId, outputGcsUri); | ||
} | ||
|
||
// Generates an image with a canny control reference image and text input | ||
public static Optional<String> generateImageWithCannyCtrl(String modelId, String outputGcsUri) { | ||
// Client Initialization. Once created, it can be reused for multiple requests. | ||
try (Client client = Client.builder().location("global").vertexAI(true).build()) { | ||
// Create a reference image out of an existing canny edge image signal | ||
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_canny.png | ||
ControlReferenceImage controlReferenceImage = | ||
ControlReferenceImage.builder() | ||
.referenceId(1) | ||
.referenceImage( | ||
Image.builder() | ||
.gcsUri("gs://cloud-samples-data/generative-ai/image/car_canny.png") | ||
.build()) | ||
.config(ControlReferenceConfig.builder().controlType("CONTROL_TYPE_CANNY").build()) | ||
.build(); | ||
|
||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
EditImageResponse imageResponse = | ||
client.models.editImage( | ||
modelId, | ||
"a watercolor painting of a red car[1] driving on a road", | ||
List.of(controlReferenceImage), | ||
EditImageConfig.builder() | ||
.editMode("EDIT_MODE_CONTROLLED_EDITING") | ||
.numberOfImages(1) | ||
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE") | ||
.personGeneration("ALLOW_ADULT") | ||
.outputGcsUri(outputGcsUri) | ||
.build()); | ||
|
||
Image generatedImage = | ||
imageResponse | ||
.generatedImages() | ||
.flatMap(generatedImages -> generatedImages.stream().findFirst()) | ||
.flatMap(GeneratedImage::image) | ||
.orElseThrow(() -> new IllegalStateException("No image was generated by the model.")); | ||
|
||
generatedImage.gcsUri().ifPresent(System.out::println); | ||
// Example response: | ||
// gs://your-bucket/your-prefix | ||
|
||
return generatedImage.gcsUri(); | ||
} | ||
} | ||
} | ||
// [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img] |
84 changes: 84 additions & 0 deletions
84
genai/snippets/src/main/java/genai/imagegeneration/ImageGenRawReferenceWithTextAndImage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package genai.imagegeneration; | ||
|
||
// [START googlegenaisdk_imggen_raw_reference_with_txt_img] | ||
|
||
import com.google.genai.Client; | ||
import com.google.genai.types.EditImageConfig; | ||
import com.google.genai.types.EditImageResponse; | ||
import com.google.genai.types.GeneratedImage; | ||
import com.google.genai.types.Image; | ||
import com.google.genai.types.RawReferenceImage; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class ImageGenRawReferenceWithTextAndImage { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String modelId = "imagen-3.0-capability-001"; | ||
String outputGcsUri = "gs://your-bucket/your-prefix"; | ||
generateImageWithRawReference(modelId, outputGcsUri); | ||
} | ||
|
||
// Generates an image with a raw reference image and text input | ||
public static Optional<String> generateImageWithRawReference( | ||
String modelId, String outputGcsUri) { | ||
// Client Initialization. Once created, it can be reused for multiple requests. | ||
try (Client client = Client.builder().location("global").vertexAI(true).build()) { | ||
// Create a raw reference image of teacup stored in Google Cloud Storage | ||
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/teacup-1.png | ||
RawReferenceImage rawReferenceImage = | ||
RawReferenceImage.builder() | ||
.referenceId(1) | ||
.referenceImage( | ||
Image.builder() | ||
.gcsUri("gs://cloud-samples-data/generative-ai/image/teacup-1.png") | ||
.build()) | ||
.build(); | ||
|
||
EditImageResponse imageResponse = | ||
client.models.editImage( | ||
modelId, | ||
"transform the subject in the image so that " | ||
+ "the teacup[1] is made entirely out of chocolate", | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
List.of(rawReferenceImage), | ||
EditImageConfig.builder() | ||
.editMode("EDIT_MODE_DEFAULT") | ||
.numberOfImages(1) | ||
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE") | ||
.personGeneration("ALLOW_ADULT") | ||
.outputGcsUri(outputGcsUri) | ||
.build()); | ||
|
||
Image generatedImage = | ||
imageResponse | ||
.generatedImages() | ||
.flatMap(generatedImages -> generatedImages.stream().findFirst()) | ||
.flatMap(GeneratedImage::image) | ||
.orElseThrow(() -> new IllegalStateException("No image was generated by the model.")); | ||
|
||
generatedImage.gcsUri().ifPresent(System.out::println); | ||
// Example response: | ||
// gs://your-bucket/your-prefix | ||
|
||
return generatedImage.gcsUri(); | ||
} | ||
} | ||
} | ||
// [END googlegenaisdk_imggen_raw_reference_with_txt_img] |
86 changes: 86 additions & 0 deletions
86
...nippets/src/main/java/genai/imagegeneration/ImageGenScribbleCtrlTypeWithTextAndImage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package genai.imagegeneration; | ||
|
||
// [START googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img] | ||
|
||
import com.google.genai.Client; | ||
import com.google.genai.types.ControlReferenceConfig; | ||
import com.google.genai.types.ControlReferenceImage; | ||
import com.google.genai.types.EditImageConfig; | ||
import com.google.genai.types.EditImageResponse; | ||
import com.google.genai.types.GeneratedImage; | ||
import com.google.genai.types.Image; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class ImageGenScribbleCtrlTypeWithTextAndImage { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String modelId = "imagen-3.0-capability-001"; | ||
String outputGcsUri = "gs://your-bucket/your-prefix"; | ||
generateImageWithScribbleCtrl(modelId, outputGcsUri); | ||
} | ||
|
||
// Generates an image with a scribble control reference image and text input | ||
public static Optional<String> generateImageWithScribbleCtrl( | ||
String modelId, String outputGcsUri) { | ||
// Client Initialization. Once created, it can be reused for multiple requests. | ||
try (Client client = Client.builder().location("global").vertexAI(true).build()) { | ||
// Create a reference image out of an existing scribble image signal | ||
// using | ||
// https://storage.googleapis.com/cloud-samples-data/generative-ai/image/car_scribble.png | ||
ControlReferenceImage controlReferenceImage = | ||
ControlReferenceImage.builder() | ||
.referenceId(1) | ||
.referenceImage( | ||
Image.builder() | ||
.gcsUri("gs://cloud-samples-data/generative-ai/image/car_scribble.png") | ||
.build()) | ||
.config(ControlReferenceConfig.builder().controlType("CONTROL_TYPE_SCRIBBLE").build()) | ||
.build(); | ||
|
||
EditImageResponse imageResponse = | ||
client.models.editImage( | ||
modelId, | ||
"an oil painting showing the side of a red car[1]", | ||
List.of(controlReferenceImage), | ||
EditImageConfig.builder() | ||
.editMode("EDIT_MODE_CONTROLLED_EDITING") | ||
.numberOfImages(1) | ||
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE") | ||
.personGeneration("ALLOW_ADULT") | ||
.outputGcsUri(outputGcsUri) | ||
.build()); | ||
|
||
Image generatedImage = | ||
imageResponse | ||
.generatedImages() | ||
.flatMap(generatedImages -> generatedImages.stream().findFirst()) | ||
.flatMap(GeneratedImage::image) | ||
.orElseThrow(() -> new IllegalStateException("No image was generated by the model.")); | ||
|
||
generatedImage.gcsUri().ifPresent(System.out::println); | ||
// Example response: | ||
// gs://your-bucket/your-prefix | ||
|
||
return generatedImage.gcsUri(); | ||
} | ||
} | ||
} | ||
// [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img] |
85 changes: 85 additions & 0 deletions
85
.../snippets/src/main/java/genai/imagegeneration/ImageGenStyleReferenceWithTextAndImage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package genai.imagegeneration; | ||
|
||
// [START googlegenaisdk_imggen_style_reference_with_txt_img] | ||
|
||
import com.google.genai.Client; | ||
import com.google.genai.types.EditImageConfig; | ||
import com.google.genai.types.EditImageResponse; | ||
import com.google.genai.types.GeneratedImage; | ||
import com.google.genai.types.Image; | ||
import com.google.genai.types.StyleReferenceConfig; | ||
import com.google.genai.types.StyleReferenceImage; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class ImageGenStyleReferenceWithTextAndImage { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String modelId = "imagen-3.0-capability-001"; | ||
String outputGcsUri = "gs://your-bucket/your-prefix"; | ||
generateImageWithStyleReference(modelId, outputGcsUri); | ||
} | ||
|
||
// Generates an image with a style reference image and text input | ||
public static Optional<String> generateImageWithStyleReference( | ||
String modelId, String outputGcsUri) { | ||
// Client Initialization. Once created, it can be reused for multiple requests. | ||
try (Client client = Client.builder().location("global").vertexAI(true).build()) { | ||
// Create a style reference image of a neon sign stored in Google Cloud Storage | ||
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png | ||
StyleReferenceImage styleReferenceImage = | ||
StyleReferenceImage.builder() | ||
.referenceId(1) | ||
.referenceImage( | ||
Image.builder() | ||
.gcsUri("gs://cloud-samples-data/generative-ai/image/neon.png") | ||
.build()) | ||
.config(StyleReferenceConfig.builder().styleDescription("neon sign").build()) | ||
.build(); | ||
|
||
EditImageResponse imageResponse = | ||
client.models.editImage( | ||
modelId, | ||
"generate an image of a neon sign [1] with the words: have a great day", | ||
List.of(styleReferenceImage), | ||
EditImageConfig.builder() | ||
.editMode("EDIT_MODE_DEFAULT") | ||
.numberOfImages(1) | ||
.safetyFilterLevel("BLOCK_MEDIUM_AND_ABOVE") | ||
.personGeneration("ALLOW_ADULT") | ||
.outputGcsUri(outputGcsUri) | ||
.build()); | ||
|
||
Image generatedImage = | ||
imageResponse | ||
.generatedImages() | ||
.flatMap(generatedImages -> generatedImages.stream().findFirst()) | ||
.flatMap(GeneratedImage::image) | ||
.orElseThrow(() -> new IllegalStateException("No image was generated by the model.")); | ||
|
||
generatedImage.gcsUri().ifPresent(System.out::println); | ||
// Example response: | ||
// gs://your-bucket/your-prefix | ||
|
||
return generatedImage.gcsUri(); | ||
} | ||
} | ||
} | ||
// [END googlegenaisdk_imggen_style_reference_with_txt_img] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.