Skip to content

Commit d2994bf

Browse files
authored
feat: (genai) add upscale sample for Imagen 4 (#13631)
* feat: add upscale sample for Imagen 4 * fix: function name * fix: args
1 parent 32f7002 commit d2994bf

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
from google.genai.types import Image
16+
17+
18+
def upscale_images(output_file: str) -> Image:
19+
# [START googlegenaisdk_imggen_upscale_with_img]
20+
from google import genai
21+
from google.genai.types import Image
22+
23+
client = genai.Client()
24+
25+
# TODO(developer): Update and un-comment below line
26+
# output_file = "output-image.png"
27+
28+
image = client.models.upscale_image(
29+
model="imagen-4.0-upscale-preview",
30+
image=Image.from_file(location="test_resources/dog_newspaper.png"),
31+
upscale_factor="x2",
32+
)
33+
34+
image.generated_images[0].image.save(output_file)
35+
36+
print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
37+
# Example response:
38+
# Created output image using 1234567 bytes
39+
40+
# [END googlegenaisdk_imggen_upscale_with_img]
41+
return image.generated_images[0].image
42+
43+
44+
if __name__ == "__main__":
45+
upscale_images(output_file="output_folder/dog_newspaper.png")

genai/image_generation/test_image_generation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import imggen_scribble_ctrl_type_with_txt_img
3838
import imggen_style_reference_with_txt_img
3939
import imggen_subj_refer_ctrl_refer_with_txt_imgs
40+
import imggen_upscale_with_img
4041
import imggen_virtual_try_on_with_txt_img
4142
import imggen_with_txt
4243

@@ -147,3 +148,9 @@ def test_img_virtual_try_on() -> None:
147148
OUTPUT_FILE = os.path.join(RESOURCES, "man_in_sweater.png")
148149
response = imggen_virtual_try_on_with_txt_img.virtual_try_on(OUTPUT_FILE)
149150
assert response
151+
152+
153+
def test_img_upscale() -> None:
154+
OUTPUT_FILE = os.path.join(RESOURCES, "dog_newspaper.png")
155+
response = imggen_upscale_with_img.upscale_images(OUTPUT_FILE)
156+
assert response

0 commit comments

Comments
 (0)