|
| 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") |
0 commit comments