Skip to content

Commit fe00be5

Browse files
author
Guiners
committed
adding imggen samples
1 parent 5fe1ec4 commit fe00be5

13 files changed

+677
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
18+
const {GoogleGenAI, ControlReferenceImage} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION =
22+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
23+
24+
async function generateImage(
25+
outputGcsUri,
26+
projectId = GOOGLE_CLOUD_PROJECT,
27+
location = GOOGLE_CLOUD_LOCATION
28+
) {
29+
const client = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
const controlReferenceImage = new ControlReferenceImage();
36+
controlReferenceImage.referenceId = 1;
37+
controlReferenceImage.referenceImage = {
38+
gcsUri: 'gs://cloud-samples-data/generative-ai/image/car_canny.png',
39+
};
40+
controlReferenceImage.config = {
41+
controlType: 'CONTROL_TYPE_CANNY',
42+
};
43+
44+
// TODO(developer): Update and un-comment below line
45+
// outputGcsUri = "gs://your-bucket/your-prefix"
46+
47+
const response = await client.models.editImage({
48+
model: 'imagen-3.0-capability-001',
49+
prompt: 'a watercolor painting of a red car[1] driving on a road',
50+
referenceImages: [controlReferenceImage],
51+
config: {
52+
editMode: 'EDIT_MODE_CONTROLLED_EDITING',
53+
numberOfImages: 1,
54+
safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE',
55+
personGeneration: 'ALLOW_ADULT',
56+
outputGcsUri: outputGcsUri,
57+
},
58+
});
59+
console.log(response.generatedImages);
60+
61+
// Example response:
62+
// gs://your-bucket/your-prefix
63+
return response.generatedImages;
64+
}
65+
// [END googlegenaisdk_imggen_canny_ctrl_type_with_txt_img]
66+
67+
module.exports = {
68+
generateImage,
69+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START googlegenaisdk_imggen_raw_reference_with_txt_img]
16+
17+
'use strict';
18+
19+
const {GoogleGenAI, RawReferenceImage} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION =
23+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
async function generateImage(
26+
outputGcsUri,
27+
projectId = GOOGLE_CLOUD_PROJECT,
28+
location = GOOGLE_CLOUD_LOCATION
29+
) {
30+
const client = new GoogleGenAI({
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
const referenceImages = new RawReferenceImage();
36+
referenceImages.referenceId = 1;
37+
referenceImages.referenceImage = {
38+
gcsUri: 'gs://cloud-samples-data/generative-ai/image/teacup-1.png',
39+
};
40+
41+
// TODO(developer): Update and un-comment below line
42+
// outputGcsUri = "gs://your-bucket/your-prefix"
43+
44+
const response = await client.models.editImage({
45+
model: 'imagen-3.0-capability-001',
46+
prompt:
47+
'transform the subject in the image so that the teacup[1] is made entirely out of chocolate',
48+
referenceImages: [referenceImages],
49+
config: {
50+
editMode: 'EDIT_MODE_DEFAULT',
51+
numberOfImages: 1,
52+
safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE',
53+
personGeneration: 'ALLOW_ADULT',
54+
outputGcsUri: outputGcsUri,
55+
},
56+
});
57+
58+
console.log(response);
59+
60+
// Example response:
61+
// gs://your-bucket/your-prefix
62+
63+
return response.generatedImages[0].image.gcsUri;
64+
}
65+
// [END googlegenaisdk_imggen_raw_reference_with_txt_img]
66+
67+
module.exports = {
68+
generateImage,
69+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
18+
const {GoogleGenAI, ControlReferenceImage} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION =
22+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
23+
24+
async function generateImage(
25+
outputGcsUri,
26+
projectId = GOOGLE_CLOUD_PROJECT,
27+
location = GOOGLE_CLOUD_LOCATION
28+
) {
29+
const client = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
const controlReferenceImage = new ControlReferenceImage();
36+
controlReferenceImage.referenceId = 1;
37+
controlReferenceImage.referenceImage = {
38+
gcsUri: 'gs://cloud-samples-data/generative-ai/image/car_scribble.png',
39+
};
40+
controlReferenceImage.config = {
41+
controlType: 'CONTROL_TYPE_SCRIBBLE',
42+
};
43+
44+
// TODO(developer): Update and un-comment below line
45+
// outputGcsUri = "gs://your-bucket/your-prefix"
46+
47+
const response = await client.models.editImage({
48+
model: 'imagen-3.0-capability-001',
49+
prompt: 'an oil painting showing the side of a red car[1]',
50+
referenceImages: [controlReferenceImage],
51+
config: {
52+
editMode: 'EDIT_MODE_CONTROLLED_EDITING',
53+
numberOfImages: 1,
54+
safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE',
55+
personGeneration: 'ALLOW_ADULT',
56+
outputGcsUri: outputGcsUri,
57+
},
58+
});
59+
console.log(response.generatedImages);
60+
61+
// Example response:
62+
// gs://your-bucket/your-prefix
63+
return response.generatedImages;
64+
}
65+
// [END googlegenaisdk_imggen_scribble_ctrl_type_with_txt_img]
66+
67+
module.exports = {
68+
generateImage,
69+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_imggen_style_reference_with_txt_img]
18+
const {GoogleGenAI, StyleReferenceImage} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION =
22+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
23+
24+
async function generateImage(
25+
outputGcsUri,
26+
projectId = GOOGLE_CLOUD_PROJECT,
27+
location = GOOGLE_CLOUD_LOCATION
28+
) {
29+
const client = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
// Create a style reference image of a neon sign stored in Google Cloud Storage
36+
// using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png
37+
38+
// TODO(developer): Update and un-comment below line
39+
// outputGcsUri = "gs://your-bucket/your-prefix"
40+
41+
const styleReferenceImage = new StyleReferenceImage();
42+
styleReferenceImage.referenceId = 1;
43+
styleReferenceImage.referenceImage = {
44+
gcsUri: 'gs://cloud-samples-data/generative-ai/image/neon.png',
45+
};
46+
styleReferenceImage.config = {
47+
styleDescription: 'neon sign',
48+
};
49+
50+
const response = await client.models.editImage({
51+
model: 'imagen-3.0-capability-001',
52+
prompt:
53+
'generate an image of a neon sign [1] with the words: have a great day',
54+
referenceImages: [styleReferenceImage],
55+
config: {
56+
editMode: 'EDIT_MODE_CONTROLLED_EDITING',
57+
numberOfImages: 1,
58+
safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE',
59+
personGeneration: 'ALLOW_ADULT',
60+
outputGcsUri: outputGcsUri,
61+
},
62+
});
63+
console.log(response.generatedImages);
64+
65+
// Example response:
66+
// gs://your-bucket/your-prefix
67+
return response.generatedImages;
68+
}
69+
// [END googlegenaisdk_imggen_style_reference_with_txt_img]
70+
71+
module.exports = {
72+
generateImage,
73+
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// [START googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]
18+
const {
19+
GoogleGenAI,
20+
ControlReferenceImage,
21+
SubjectReferenceImage,
22+
} = require('@google/genai');
23+
24+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
25+
const GOOGLE_CLOUD_LOCATION =
26+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
27+
28+
async function generateImage(
29+
outputGcsUri,
30+
projectId = GOOGLE_CLOUD_PROJECT,
31+
location = GOOGLE_CLOUD_LOCATION
32+
) {
33+
const client = new GoogleGenAI({
34+
vertexai: true,
35+
project: projectId,
36+
location: location,
37+
});
38+
39+
const subjectReferenceImage = new SubjectReferenceImage();
40+
subjectReferenceImage.referenceId = 1;
41+
subjectReferenceImage.referenceImage = {
42+
gcsUri: 'gs://cloud-samples-data/generative-ai/image/person.png',
43+
};
44+
subjectReferenceImage.config = {
45+
subjectDescription: 'a headshot of a woman',
46+
subjectType: 'SUBJECT_TYPE_PERSON',
47+
};
48+
49+
const controlReferenceImage = new ControlReferenceImage();
50+
controlReferenceImage.referenceId = 2;
51+
controlReferenceImage.referenceImage = {
52+
gcsUri: 'gs://cloud-samples-data/generative-ai/image/person.png',
53+
};
54+
controlReferenceImage.config = {
55+
controlType: 'CONTROL_TYPE_FACE_MESH',
56+
};
57+
58+
// TODO(developer): Update and un-comment below line
59+
// outputGcsUri = "gs://your-bucket/your-prefix"
60+
61+
const response = await client.models.editImage({
62+
model: 'imagen-3.0-capability-001',
63+
prompt:
64+
'a portrait of a woman[1] in the pose of the control image[2]in a watercolor style by a professional artist,\n' +
65+
' light and low-contrast stokes, bright pastel colors, a warm atmosphere, clean background, grainy paper,\n' +
66+
' bold visible brushstrokes, patchy details',
67+
referenceImages: [subjectReferenceImage, controlReferenceImage],
68+
config: {
69+
editMode: 'EDIT_MODE_CONTROLLED_EDITING',
70+
numberOfImages: 1,
71+
safetyFilterLevel: 'BLOCK_MEDIUM_AND_ABOVE',
72+
personGeneration: 'ALLOW_ADULT',
73+
outputGcsUri: outputGcsUri,
74+
},
75+
});
76+
console.log(response.generatedImages);
77+
78+
// Example response:
79+
// gs://your-bucket/your-prefix
80+
return response.generatedImages;
81+
}
82+
// [END googlegenaisdk_imggen_subj_refer_ctrl_refer_with_txt_imgs]
83+
84+
module.exports = {
85+
generateImage,
86+
};

0 commit comments

Comments
 (0)