Skip to content

Commit a019a79

Browse files
authored
feat: new image model sample update (#13382)
1 parent 5faafd1 commit a019a79

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

genai/image_generation/imggen_with_txt.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,32 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from google.genai.types import Image
1516

16-
def generate_images(output_gcs_uri: str) -> str:
17+
18+
def generate_images(output_file: str) -> Image:
1719
# [START googlegenaisdk_imggen_with_txt]
1820
from google import genai
19-
from google.genai.types import GenerateImagesConfig
2021

2122
client = genai.Client()
2223

2324
# TODO(developer): Update and un-comment below line
24-
# output_gcs_uri = "gs://your-bucket/your-prefix"
25+
# output_file = "output-image.png"
2526

2627
image = client.models.generate_images(
27-
model="imagen-3.0-generate-002",
28+
model="imagen-4.0-generate-preview-05-20",
2829
prompt="A dog reading a newspaper",
29-
config=GenerateImagesConfig(
30-
aspect_ratio="1:1",
31-
number_of_images=1,
32-
safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
33-
person_generation="DONT_ADULT",
34-
output_gcs_uri=output_gcs_uri,
35-
),
3630
)
3731

32+
image.generated_images[0].image.save(output_file)
33+
34+
print(f"Created output image using {len(image.generated_images[0].image.image_bytes)} bytes")
3835
# Example response:
39-
# gs://your-bucket/your-prefix
40-
print(image.generated_images[0].image.gcs_uri)
36+
# Created output image using 1234567 bytes
37+
4138
# [END googlegenaisdk_imggen_with_txt]
42-
return image.generated_images[0].image.gcs_uri
39+
return image.generated_images[0].image
4340

4441

4542
if __name__ == "__main__":
46-
generate_images(output_gcs_uri="gs://your-bucket/your-prefix")
43+
generate_images(output_file="test_resources/dog_newspaper.png")

genai/image_generation/test_image_generation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
4242

4343
GCS_OUTPUT_BUCKET = "python-docs-samples-tests"
44+
RESOURCES = os.path.join(os.path.dirname(__file__), "test_resources")
45+
OUTPUT_FILE = os.path.join(RESOURCES, "dog_newspaper.png")
4446

4547

4648
@pytest.fixture(scope="session")
@@ -58,7 +60,7 @@ def output_gcs_uri() -> str:
5860

5961
def test_img_generation(output_gcs_uri: str) -> None:
6062
response = imggen_with_txt.generate_images(
61-
output_gcs_uri=output_gcs_uri
63+
OUTPUT_FILE
6264
)
6365
assert response
6466

841 KB
Loading

0 commit comments

Comments
 (0)