I cannot get Image Edit to work on mages made by Image


Following the examples

curl https://api.openai.com/v1/images/generations -H “Content-Type: application/json” -H “Authorization: Bearer $OPENAI_API_KEY” -d ‘{“prompt”: “A cute baby sea otter”,“n”: 1, “size”: “1024x1024” }’

The only change from the example is I changed n to be 1

I then downloaded the image generated and named the file and renamed it “otter.png”

Then ran the example code (again n is 1, and I am no supplying a mask)

curl https://api.openai.com/v1/images/edits -H “Authorization: Bearer $OPENAI_API_KEY” -F image=“@otter.png” -F prompt=“A cute baby sea otter wearing a beret” -F n=1 -F size=“1024x1024”

I get the error:

/tmp$ curl https://api.openai.com/v1/images/edits  -H "Authorization: Bearer $OPENAI_API_KEY" -F image="@otter.png"  -F prompt="A cute baby sea otter wearing a beret" -F n=1 -F size="1024x1024"
  "error": {
    "code": null,
    "message": "Invalid input image - format must be in ['RGBA', 'LA', 'L'], got RGB.",
    "param": null,
    "type": "invalid_request_error"

Does this mean what I think it says, that the image format used by the images/generations is incompatible with the images/edits API?

Finaly the command:

curl https://api.openai.com/v1/images/variations -H “Authorization: Bearer $OPENAI_API_KEY” -F image=“@otter.png” -F n=1 -F size=“1024x1024”

works well. SO the image format is compatible images/variations

What am I missing?

It is possible to convert the image:

convert otter.png -type TrueColor -define png:color-type=6 otter_rgba.png


I tried this too… Ran curl to generate the otter image and then ran it to edit the image. Both completed without error, but the otter in the edited image had no hat.

Here’s the generate command:

curl https://api.openai.com/v1/images/generations -H “Content-Type: application/json” -H “Authorization: Bearer MYAPIKEY” -d “{ "prompt": "A cute baby sea otter", "n": 2, "size": "1024x1024" }”

Here’s the edit command:

@echo off
curl https://api.openai.com/v1/images/edits ^
  -H "Authorization: Bearer MYAPIKEY" ^
  -F image="@sea_otter_rgba.png" ^
  -F prompt="Add a hat to the otter" ^
  -F n=2 ^
  -F size="1024x1024" > output.json

No error messages were returned, which suggests correct syntax was used. So, something else is going on. Since these commands come from the API reference, I was hoping the simple starter examples would work. Once these curl examples are working, plan to do the same in Python.

1 Like

I don’t think I ever got it working with curl and ended up using the library… It has something to do with the filetype being created, though, IIRC - the alpha channel?

$patha = curl_file_create(‘./256_image.png’);
$pathb = curl_file_create(‘./256_mask.png’);

 $result = $open_ai->imageEdit([
 "image" => $patha,
 "mask" => $pathb,
 "prompt" => $prompt,
 "n" => $numImages,
 "size" => $finalSize,
 "response_format" => $response
> ]);

If you get it sorted out with curl alone, please let us know!

1 Like

Here’s what docs state:

The image edits endpoint allows you to edit and extend an image by uploading a mask. The transparent areas of the mask indicate where the image should be edited, and the prompt should describe the full new image, not just the erased area . This endpoint can enable experiences like the editor in our DALL·E preview app.

1 Like

Thanks Paul, I think i’ll drop Curl, and try using another language… The code you pasted looks like PHP? Is that worth a try? I was thinking to try Python.

Yeah, it’s PHP.

I don’t think it’s the language as much as using the library that helped. Although each language deals with images slightly differently…

Please come back and let us know. Good luck.

I started this thread and I neglected to mark my solution - I had to adjust the format

That said…

Pedantically curl is not a language, it is a programme

My advice is to use curl to prototype using the API. There is a small amount of command syntax to learn, about five things in total. There is nothing to get in your way.

If you cannot get an API call working with curl then you have a misunderstanding.

Stick with curl on the command line and switch out to programming when you thoroughly understand how it works.

1 Like

Thanks. How did you adjust the format ? Here’s the curl command I’m using:

@echo off
:: Scriptname: openai_curl_image_edit.bat
set PROMPT_TEXT=“A white siamese cat wearing a red hat”
curl https://api.openai.com/v1/images/edits ^
-H “Authorization: Bearer ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■g8piZeCX” ^
-F image=“@white_cat_rgba.png” ^
-F prompt=%PROMPT_TEXT% ^
-F n=2 ^
-F size=“1024x1024” > output.json
type output.json

After changing format of the input file to RGBA, no error messages were generated but the Siamese cat doesn’t get a hat. Maybe need to add a mask file but need to figure out how to make those.

1 Like

convert otter.png -type TrueColor -define png:color-type=6 otter_rgba.png

convert is from Image Magick

That changes the image from 24 bit to 32 bit, but doesn’t actually draw the transparency for you in the alpha channel of the image:

If mask is not provided, image must have transparency, which will be used as the mask.

The AI has to know where it is allowed to paint new stuff, and where it should leave the image unaltered.

Pixel transparency should be fully “on” or fully transparent, not fading as a photoshop erase tool does by default.

I offered gimp links in this similar topic:

I made a mask in Gimp.


@mustmolenberg So, to summarize, it sounds like you did two things:

  1. You converted the image from 24 bit to 32 bit (rgba format) using Image Magick.

  2. You used Gimp to draw a transparency mask into the alpha channel of the mask image to tell the AI where its allowed to paint new stuff.

Does that sound right?

1 Like



This is how I got it working with CURL only :slight_smile:

$image = new CURLFile("reception.png");
$mask = new CURLFile("mask.png");

$data = array(
	"mask" => $mask,
	"image" => $image,
	"model" => "dall-e-2",
	"prompt" => "add a nice white cat",
	"n" => 1,
	"size" => "1024x1024",

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/images/edits');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
	'Authorization: Bearer YOUR API KEY',
	'Content-Type: multipart/form-data',
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);

It took me 1 hour to figure out how to do it, so hopefully it will save someone’s time.


Well done. Great first post. Hope you stick around our growing community.

1 Like

As this topic has a selected solution, closing topic.