-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Adding imggen samples #4193
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
Guiners
wants to merge
14
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/imggen
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fe00be5
adding imggen samples
24aea6b
Merge branch 'main' into sample/imggen
Guiners 728ad92
adding license
956b236
Merge remote-tracking branch 'origin/sample/imggen' into sample/imggen
1381b08
Update genai/image-generation/imggen-subj-refer-ctrl-refer-with-txt-i…
Guiners 4ee6e73
Update genai/test/imggen-raw-reference-with-txt-img.test.js
Guiners 7da5c3d
codereview fix
9930696
Merge remote-tracking branch 'origin/sample/imggen' into sample/imggen
c1a6add
codereview fix
e521e1a
fixing failed tests
3ae0039
Merge branch 'main' into sample/imggen
gericdong e882b5c
codereview fix
585f5cd
Merge remote-tracking branch 'origin/sample/imggen' into sample/imggen
fa57672
fixing tests
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
genai/image-generation/imggen-canny-ctrl-type-with-txt-img.js
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,70 @@ | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img] | ||
| const {GoogleGenAI, ControlReferenceImage} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = | ||
| process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
|
||
| async function generateImage( | ||
| outputGcsUri, | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| }); | ||
|
|
||
| const controlReferenceImage = new ControlReferenceImage(); | ||
| controlReferenceImage.referenceId = 1; | ||
| controlReferenceImage.referenceImage = { | ||
| gcsUri: 'gs://cloud-samples-data/generative-ai/image/car_canny.png', | ||
| }; | ||
| controlReferenceImage.config = { | ||
| controlType: 'CONTROL_TYPE_CANNY', | ||
| }; | ||
|
|
||
| // TODO(developer): Update and un-comment below line | ||
| // outputGcsUri = "gs://your-bucket/your-prefix" | ||
|
|
||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const response = await client.models.editImage({ | ||
| model: 'imagen-3.0-capability-001', | ||
| // The '[1]' in the prompt corresponds to the controlReferenceImage.referenceId above. | ||
| prompt: 'a watercolor painting of a red car[1] driving on a road', | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| referenceImages: [controlReferenceImage], | ||
| config: { | ||
| editMode: 'EDIT_MODE_CONTROLLED_EDITING', | ||
| numberOfImages: 1, | ||
| safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', | ||
| personGeneration: 'ALLOW_ADULT', | ||
| outputGcsUri: outputGcsUri, | ||
| }, | ||
| }); | ||
|
|
||
| console.log(response.generatedImages[0].image.gcsUri); | ||
| // Example response: | ||
| // gs://your-bucket/your-prefix | ||
| return response.generatedImages[0].image.gcsUri; | ||
| } | ||
| // [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img] | ||
|
|
||
| module.exports = { | ||
| generateImage, | ||
| }; | ||
70 changes: 70 additions & 0 deletions
70
genai/image-generation/imggen-raw-reference-with-txt-img.js
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,70 @@ | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| // [START googlegenaisdk_imggen_raw_reference_with_txt_img] | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const {GoogleGenAI, RawReferenceImage} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = | ||
| process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
|
||
| async function generateImage( | ||
| outputGcsUri, | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| }); | ||
|
|
||
| const referenceImages = new RawReferenceImage(); | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| referenceImages.referenceId = 1; | ||
| referenceImages.referenceImage = { | ||
| gcsUri: 'gs://cloud-samples-data/generative-ai/image/teacup-1.png', | ||
| }; | ||
|
|
||
| // TODO(developer): Update and un-comment below line | ||
| // outputGcsUri = "gs://your-bucket/your-prefix" | ||
|
|
||
| const response = await client.models.editImage({ | ||
| model: 'imagen-3.0-capability-001', | ||
| prompt: | ||
| 'transform the subject in the image so that the teacup[1] is made entirely out of chocolate', | ||
| referenceImages: [referenceImages], | ||
| config: { | ||
| editMode: 'EDIT_MODE_DEFAULT', | ||
| numberOfImages: 1, | ||
| safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', | ||
| personGeneration: 'ALLOW_ADULT', | ||
| outputGcsUri: outputGcsUri, | ||
| }, | ||
| }); | ||
|
|
||
| console.log(response); | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Example response: | ||
| // gs://your-bucket/your-prefix | ||
|
|
||
| return response.generatedImages[0].image.gcsUri; | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| // [END googlegenaisdk_imggen_raw_reference_with_txt_img] | ||
|
|
||
| module.exports = { | ||
| generateImage, | ||
| }; | ||
69 changes: 69 additions & 0 deletions
69
genai/image-generation/imggen-scribble-ctrl-type-with-txt-img.js
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,69 @@ | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img] | ||
| const {GoogleGenAI, ControlReferenceImage} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = | ||
| process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
|
||
| async function generateImage( | ||
| outputGcsUri, | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| }); | ||
|
|
||
| const controlReferenceImage = new ControlReferenceImage(); | ||
| controlReferenceImage.referenceId = 1; | ||
| controlReferenceImage.referenceImage = { | ||
| gcsUri: 'gs://cloud-samples-data/generative-ai/image/car_scribble.png', | ||
| }; | ||
| controlReferenceImage.config = { | ||
| controlType: 'CONTROL_TYPE_SCRIBBLE', | ||
| }; | ||
|
|
||
| // TODO(developer): Update and un-comment below line | ||
| // outputGcsUri = "gs://your-bucket/your-prefix" | ||
|
|
||
| const response = await client.models.editImage({ | ||
| model: 'imagen-3.0-capability-001', | ||
| prompt: 'an oil painting showing the side of a red car[1]', | ||
| referenceImages: [controlReferenceImage], | ||
| config: { | ||
| editMode: 'EDIT_MODE_CONTROLLED_EDITING', | ||
| numberOfImages: 1, | ||
| safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', | ||
| personGeneration: 'ALLOW_ADULT', | ||
| outputGcsUri: outputGcsUri, | ||
| }, | ||
| }); | ||
| console.log(response.generatedImages); | ||
|
|
||
| // Example response: | ||
| // gs://your-bucket/your-prefix | ||
| return response.generatedImages; | ||
| } | ||
| // [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img] | ||
|
|
||
| module.exports = { | ||
| generateImage, | ||
| }; |
73 changes: 73 additions & 0 deletions
73
genai/image-generation/imggen-style-reference-with-txt-img.js
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,73 @@ | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_imggen_style_reference_with_txt_img] | ||
| const {GoogleGenAI, StyleReferenceImage} = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = | ||
| process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
|
||
| async function generateImage( | ||
| outputGcsUri, | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| }); | ||
|
|
||
| // 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 | ||
|
|
||
| // TODO(developer): Update and un-comment below line | ||
| // outputGcsUri = "gs://your-bucket/your-prefix" | ||
|
|
||
| const styleReferenceImage = new StyleReferenceImage(); | ||
| styleReferenceImage.referenceId = 1; | ||
| styleReferenceImage.referenceImage = { | ||
| gcsUri: 'gs://cloud-samples-data/generative-ai/image/neon.png', | ||
| }; | ||
| styleReferenceImage.config = { | ||
| styleDescription: 'neon sign', | ||
| }; | ||
|
|
||
| const response = await client.models.editImage({ | ||
| model: 'imagen-3.0-capability-001', | ||
| prompt: | ||
| 'generate an image of a neon sign [1] with the words: have a great day', | ||
| referenceImages: [styleReferenceImage], | ||
| config: { | ||
| editMode: 'EDIT_MODE_CONTROLLED_EDITING', | ||
| numberOfImages: 1, | ||
| safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', | ||
| personGeneration: 'ALLOW_ADULT', | ||
| outputGcsUri: outputGcsUri, | ||
| }, | ||
| }); | ||
| console.log(response.generatedImages); | ||
|
|
||
| // Example response: | ||
| // gs://your-bucket/your-prefix | ||
| return response.generatedImages; | ||
| } | ||
| // [END googlegenaisdk_imggen_style_reference_with_txt_img] | ||
|
|
||
| module.exports = { | ||
| generateImage, | ||
| }; |
85 changes: 85 additions & 0 deletions
85
genai/image-generation/imggen-subj-refer-ctrl-refer-with-txt-imgs.js
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 | ||
| // | ||
| // https://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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // [START googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs] | ||
| const { | ||
| GoogleGenAI, | ||
| ControlReferenceImage, | ||
| SubjectReferenceImage, | ||
| } = require('@google/genai'); | ||
|
|
||
| const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT; | ||
| const GOOGLE_CLOUD_LOCATION = | ||
| process.env.GOOGLE_CLOUD_LOCATION || 'us-central1'; | ||
|
|
||
| async function generateImage( | ||
| outputGcsUri, | ||
| projectId = GOOGLE_CLOUD_PROJECT, | ||
| location = GOOGLE_CLOUD_LOCATION | ||
| ) { | ||
| const client = new GoogleGenAI({ | ||
| vertexai: true, | ||
| project: projectId, | ||
| location: location, | ||
| }); | ||
|
|
||
| const subjectReferenceImage = new SubjectReferenceImage(); | ||
| subjectReferenceImage.referenceId = 1; | ||
| subjectReferenceImage.referenceImage = { | ||
| gcsUri: 'gs://cloud-samples-data/generative-ai/image/person.png', | ||
| }; | ||
| subjectReferenceImage.config = { | ||
| subjectDescription: 'a headshot of a woman', | ||
| subjectType: 'SUBJECT_TYPE_PERSON', | ||
| }; | ||
|
|
||
| const controlReferenceImage = new ControlReferenceImage(); | ||
| controlReferenceImage.referenceId = 2; | ||
| controlReferenceImage.referenceImage = { | ||
| gcsUri: 'gs://cloud-samples-data/generative-ai/image/person.png', | ||
| }; | ||
| controlReferenceImage.config = { | ||
| controlType: 'CONTROL_TYPE_FACE_MESH', | ||
| }; | ||
|
|
||
| // TODO(developer): Update and un-comment below line | ||
| // outputGcsUri = "gs://your-bucket/your-prefix" | ||
|
|
||
| const response = await client.models.editImage({ | ||
| model: 'imagen-3.0-capability-001', | ||
| prompt: `a portrait of a woman[1] in the pose of the control image[2]in a watercolor style by a professional artist, | ||
| light and low-contrast stokes, bright pastel colors, a warm atmosphere, clean background, grainy paper, | ||
| bold visible brushstrokes, patchy details`, | ||
| referenceImages: [subjectReferenceImage, controlReferenceImage], | ||
| config: { | ||
| editMode: 'EDIT_MODE_CONTROLLED_EDITING', | ||
| numberOfImages: 1, | ||
| safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE', | ||
| personGeneration: 'ALLOW_ADULT', | ||
| outputGcsUri: outputGcsUri, | ||
| }, | ||
| }); | ||
| console.log(response.generatedImages); | ||
|
|
||
| // Example response: | ||
| // gs://your-bucket/your-prefix | ||
| return response.generatedImages; | ||
| } | ||
| // [END googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs] | ||
|
|
||
| module.exports = { | ||
| generateImage, | ||
| }; |
Oops, something went wrong.
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.