# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values# with appropriate values for your project.exportGOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECTexportGOOGLE_CLOUD_LOCATION=globalexportGOOGLE_GENAI_USE_VERTEXAI=True
fromgoogleimportgenaifromgoogle.genai.typesimportGenerateContentConfig,ModalityfromPILimportImagefromioimportBytesIOclient=genai.Client()# Using an image of Eiffel tower, with fireworks in the background.image=Image.open("test_resources/example-image-eiffel-tower.png")response=client.models.generate_content(model="gemini-2.5-flash-image-preview",contents=[image,"Edit this image to make it look like a cartoon."],config=GenerateContentConfig(response_modalities=[Modality.TEXT,Modality.IMAGE]),)forpartinresponse.candidates[0].content.parts:ifpart.text:print(part.text)elifpart.inline_data:image=Image.open(BytesIO((part.inline_data.data)))image.save("output_folder/bw-example-image.png")# Example response:# Here's the cartoon-style edit of the image:# Cartoon-style edit:# - Simplified the Eiffel Tower with bolder lines and slightly exaggerated proportions.# - Brightened and saturated the colors of the sky, fireworks, and foliage for a more vibrant, cartoonish look.# ....
REST
在終端機中執行下列指令,在目前目錄中建立或覆寫這個檔案:
curl-XPOST\-H"Authorization: Bearer $(gcloudauthprint-access-token)"\-H"Content-Type: application/json"\https://${API_ENDPOINT}:generateContent\-d'{ "contents": { "role": "USER", "parts": [ {"file_data": { "mime_type": "image/jpg", "file_uri": "<var>FILE_NAME</var>" } }, {"text": "Convert this photo to black and white, in a cartoonish style."}, ] }, "generation_config": { "response_modalities": ["TEXT", "IMAGE"], }, "safetySettings": { "method": "PROBABILITY", "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE" }, }'2>/dev/null>response.json
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Edit images with Gemini\n\n| **Preview**\n|\n|\n| This product or feature is a Generative AI Preview offering, subject to\n| the \"Pre-GA Offerings Terms\" of the\n| [Google Cloud Service Specific Terms](/terms/service-terms),\n| as well as the\n| [Additional Terms for Generative AI Preview Products](/trustedtester/aitos). For this\n| Generative AI Preview offering, Customers may elect to use it for\n| production or commercial purposes, or disclose Generated Output to\n| third-parties, and may process personal data as outlined in the\n| [Cloud Data Processing\n| Addendum](/terms/data-processing-addendum),\n| subject to the obligations and restrictions described in the agreement\n| under which you access Google Cloud. Pre-GA products are available \"as is\"\n| and might have limited support. For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\n\u003cbr /\u003e\n\nGemini 2.5 Flash Image Preview supports multiple types of image editing.\n| **Note:** Image editing and multi-turn image editing are only supported in `gemini-2.5-flash-image-preview`, not `gemini-2.5-flash`.\n\nImage editing\n-------------\n\n| To see an example of image editing with Gemini,\n| run the \"Gemini 2.5 Flash Image Generation in Vertex AI\" notebook in one of the following\n| environments:\n|\n| [Open in Colab](https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_2_5_image_gen.ipynb)\n|\n|\n| \\|\n|\n| [Open in Colab Enterprise](https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fgetting-started%2Fintro_gemini_2_5_image_gen.ipynb)\n|\n|\n| \\|\n|\n| [Open\n| in Vertex AI Workbench](https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Fgetting-started%2Fintro_gemini_2_5_image_gen.ipynb)\n|\n|\n| \\|\n|\n| [View on GitHub](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_2_5_image_gen.ipynb)\n\nGemini 2.5 Flash Image's public preview for image generation\n(`gemini-2.5-flash-image-preview`) supports the ability to edit\nimages in addition generating them. With this public preview release,\nGemini 2.5 Flash Image supports improved editing of images and\nmulti-turn editing and contains updated safety filters that provide a more\nflexible and less restrictive user experience.\n\nIt supports the following modalities and capabilities:\n\n- Image editing (text and image to image)\n\n - **Example prompt:** \"Edit this image to make it look like a cartoon\"\n - **Example prompt:** \\[image of a cat\\] + \\[image of a pillow\\] + \"Create a cross stitch of my cat on this pillow.\"\n- Multi-turn image editing (chat)\n\n - **Example prompts:** \\[upload an image of a blue car.\\] \"Turn this car into a convertible.\" \"Now change the color to yellow.\" \"Add a spoiler.\"\n\nEdit an image\n-------------\n\n### Console\n\nTo edit images:\n\n1. Open [**Vertex AI Studio \\\u003e Create prompt**](https://console.cloud.google.com/vertex-ai/studio/multimodal).\n2. Click **Switch model** and select **`gemini-2.5-flash-image-preview`** from the menu.\n3. In the **Outputs** panel, select **Image and text** from the drop-down menu.\n4. Click **Insert media** (add_photo_alternate) and select a source from the menu, then follow the dialog's instructions.\n5. Write what edits you want to make to the image in the **Write a prompt** text area.\n6. Click the **Prompt** (send) button.\n\n\nGemini will generate an edited version of the provided image based on\nyour description. This process should take a few seconds, but may be\ncomparatively slower depending on capacity.\n\n### Python\n\n#### Install\n\n```\npip install --upgrade google-genai\n```\n\n\nTo learn more, see the\n[SDK reference documentation](https://googleapis.github.io/python-genai/).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=global\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n from google import genai\n from google.genai.types import GenerateContentConfig, Modality\n from PIL import Image\n from io import BytesIO\n\n client = genai.Client()\n\n # Using an image of Eiffel tower, with fireworks in the background.\n image = Image.open(\"test_resources/example-image-eiffel-tower.png\")\n\n response = client.models.generate_content(\n model=\"gemini-2.5-flash-image-preview\",\n contents=[image, \"Edit this image to make it look like a cartoon.\"],\n config=GenerateContentConfig(response_modalities=[Modality.TEXT, Modality.IMAGE]),\n )\n for part in response.candidates[0].content.parts:\n if part.text:\n print(part.text)\n elif part.inline_data:\n image = Image.open(BytesIO((part.inline_data.data)))\n image.save(\"output_folder/bw-example-image.png\")\n # Example response:\n # Here's the cartoon-style edit of the image:\n # Cartoon-style edit:\n # - Simplified the Eiffel Tower with bolder lines and slightly exaggerated proportions.\n # - Brightened and saturated the colors of the sky, fireworks, and foliage for a more vibrant, cartoonish look.\n # ....\n\n### REST\n\n\nRun the following command in the terminal to create or overwrite this file in\nthe current directory: \n\n curl -X POST \\\n -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n -H \"Content-Type: application/json\" \\\n https://${API_ENDPOINT}:generateContent \\\n -d '{\n \"contents\": {\n \"role\": \"USER\",\n \"parts\": [\n {\"file\u003c_da\u003eta\":\u003c {\n \u003e \"mime_type\": \"image/jpg\",\n \"file_uri\": \"varFILE_NAME/var\"\n }\n },\n {\"text\": \"Convert this photo to black and white, in a cartoonish style.\"},\n ]\n\n },\n \"generation_config\": {\n \"response_modalities\": [\"TEXT\", \"IMAGE\"]\u003e,\n },\n \u003e \"safetySettings\": {\n \"method\": \"PROBABILITY\",\n \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n \"threshold\": \"BLOCK_MEDIUM_AND_ABOVE\"\n },\n }' 2/dev/null response.json\n\n| **Note:** You **must** include `responseModalities: [\"TEXT\", \"IMAGE\"]` in your configuration. Image-only output is not supported with these models.\n\n\nGemini will generate an image based on your description. This\nprocess should take a few seconds, but may be comparatively slower depending on\ncapacity.\n\n\u003cbr /\u003e\n\nMulti-turn image editing\n------------------------\n\nGemini 2.5 Flash Image Preview also supports improved multi-turn editing, allowing\nyou to respond to the model with changes after receiving an edited image response.\nThis will allow you to continue to make edits to the image conversationally.\n\nNote that is recommended to limit the entire request file size to 50MB maximum.\n\nTo test out multi-turn image editing, try our\n[Gemini 2.5 Flash Image Preview notebook](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/getting-started/intro_gemini_2_5_image_gen.ipynb)."]]