|
| 1 | +# Copyright 2024 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 | +"""Google Cloud Vertex AI sample for editing a product image. You can |
| 16 | + modify the background content but preserve the product's appearance. |
| 17 | +Example usage: |
| 18 | + python edit_image_product_image.py --project_id <project-id> \ |
| 19 | + --location <location> --input_file <filepath> \ |
| 20 | + --output_file <filepath> --prompt <text> |
| 21 | +""" |
| 22 | + |
| 23 | +# [START generativeaionvertexai_imagen_edit_image_product_image] |
| 24 | + |
| 25 | +import argparse |
| 26 | + |
| 27 | +import vertexai |
| 28 | +from vertexai.preview.vision_models import Image, ImageGenerationModel |
| 29 | + |
| 30 | + |
| 31 | +def edit_image_product_image( |
| 32 | + project_id: str, |
| 33 | + location: str, |
| 34 | + input_file: str, |
| 35 | + output_file: str, |
| 36 | + prompt: str, |
| 37 | +) -> vertexai.preview.vision_models.ImageGenerationResponse: |
| 38 | + """Edit a local image by modifying the background content. |
| 39 | + Args: |
| 40 | + project_id: Google Cloud project ID, used to initialize Vertex AI. |
| 41 | + location: Google Cloud region, used to initialize Vertex AI. |
| 42 | + input_file: Local path to the input image file. Image can be in PNG or JPEG format. |
| 43 | + output_file: Local path to the output image file. |
| 44 | + prompt: The text prompt describing what you want to see in the background.""" |
| 45 | + |
| 46 | + vertexai.init(project=project_id, location=location) |
| 47 | + |
| 48 | + model = ImageGenerationModel.from_pretrained("imagegeneration@006") |
| 49 | + base_img = Image.load_from_file(location=input_file) |
| 50 | + |
| 51 | + images = model.edit_image( |
| 52 | + base_image=base_img, |
| 53 | + prompt=prompt, |
| 54 | + edit_mode="product-image", |
| 55 | + # Optional parameters |
| 56 | + number_of_images=1, |
| 57 | + # Allows for product repositioning: "reposition" (default; moves product |
| 58 | + # to center), "fixed" (keeps product in original position) |
| 59 | + # product_position="fixed", |
| 60 | + ) |
| 61 | + |
| 62 | + images[0].save(location=output_file) |
| 63 | + |
| 64 | + # Optional. View the edited image in a notebook. |
| 65 | + # images[0].show() |
| 66 | + |
| 67 | + print(f"Created output image using {len(images[0]._image_bytes)} bytes") |
| 68 | + |
| 69 | + return images |
| 70 | + |
| 71 | + |
| 72 | +# [END generativeaionvertexai_imagen_edit_image_product_image] |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + parser = argparse.ArgumentParser() |
| 76 | + parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) |
| 77 | + parser.add_argument( |
| 78 | + "--location", |
| 79 | + help="The location in which to initialize Vertex AI.", |
| 80 | + default="us-central1", |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + "--input_file", |
| 84 | + help="The local path to the input file (e.g., 'my-input.png').", |
| 85 | + required=True, |
| 86 | + ) |
| 87 | + parser.add_argument( |
| 88 | + "--output_file", |
| 89 | + help="The local path to the output file (e.g., 'my-output.png').", |
| 90 | + required=True, |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--prompt", |
| 94 | + help="The text prompt describing what you want to insert into the background.", |
| 95 | + required=True, |
| 96 | + ) |
| 97 | + args = parser.parse_args() |
| 98 | + edit_image_product_image( |
| 99 | + args.project_id, |
| 100 | + args.location, |
| 101 | + args.input_file, |
| 102 | + args.output_file, |
| 103 | + args.prompt, |
| 104 | + ) |
0 commit comments