I am trying Create image edit API [1] with Python code below, but the image_url returned always be the same with the input image of “otter.png”, I tried many times but never get a expected image of “A cartoon baby sea otter wearing a hat”.
#####################
response = openai.Image.create_edit( image=open(“otter.png”, “rb”), mask=open(“mask.png”, “rb”), prompt=“A cartoon baby sea otter wearing a hat”,n=2, size=“512x512”)
image_url = response[‘data’][0][‘url’]
#####################
I attached the otter.png and mask.png. Can you please help to figure out my mistake?
Mike, I’m new to Gimp… got it running but now going down a rabbit hole on the steps to create a layer mask… Finding many different tutorials. Might there be a step by step guide to creating a mask for tailored for this Image Edits API use case?
You’ll want to make a fully transparent area, pixel perfect, and not fade the transparency with a paint brush tool. You are drawing on the A channel, which for AI edits can only be “yes” or “no”.
Preserving the actual image is not important, you can paint it a solid color to make the mask file more compressible.
Thanks for the GIMP instruction links, they were very useful. Using them I was able to transform an image created by ‘image/generations’ into a transparent PNG. Pretty neat.
Thanks again for the help on this. Creating a mask file in GIMP made the difference for the call of v1/images/edits from python.
Define Request Body
'image': ('white_cat_rgba.png', open(filename, 'rb')),
'mask': ('white_cat_rgba_hat.png', open(maskfilename, 'rb')),
'prompt': (None, 'A white cat wearing a london police hat'),
'size': (None, '512x512'),
'n': (None, '1'), # Number of variations to generate
}
I wonder if the AI will appreciate that it has still left a strange background the same as the mask, and fill it in by more prompt, if you also have language about infilling the background scene from the ediges.
So, the prompt was changed to “A white cat wearing a London police officer hat with the background naturally extending from the existing edges of the image” and ran it twice.
Wanted to see if the AI would interpret this as direction to change the background. Both executions returned variations of the police cap but no change to the background. Guess I’m not sure why the AI would change the background when the transparent area of the mask is the space above the cats head where the cap is drawn.
Took a closer look at ‘Cat in Hat’ and can see what, maybe, caught your eye regarding the strange background. Callout-1 shows the area that I created using GIMP. Callout-2 is something that was positioned in the background of the original image. Callout-3 shows shows an area which, to my eye, looks like the AI used Callout-2 and stretched it to fill in behind the hat and the cats head.
This is my first time playing around with these APIs, so there’s lots to learn. One observation is that the prompt which is supplied to ‘images/edits’ is highly inter-related to the image being edited and the image mask.