How can I use Create image edit API?

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?

[1] OpenAI API

Sounds like it’s not getting the mask correctly. Here’s some code from GitHub that might help you…


    def generate_from_masked_image(self, prompt, image_path):
        with open(image_path, "rb") as f:
            image_base64 = base64.b64encode(f.read())

Thank you very much for your reply. It worked when I use a 32bit mask png instead of 24bit.

I created the 32bit mask png via GIMP, the 24 bit png which does not work was created by mspaint.exe.

1 Like

Thanks for coming back to share. Hopefully it helps someone in the future.

Good luck!

1 Like