Image editing api without mask

Hi, I’m having a slight problem with creating an edit of a picture through the api.
I’m trying to edit it by using a transparent picture made with python pillow by pasting parts of a picture on a 1080x1080 background, but when I try to generate the request for the edit I get a response “Missing 1 required positional argument: ‘mask’”.

Now I might be able to fix this by uploading a mask as well, but the documentation says that you should be able to do it without it, so I was wondering if anyone else has had the same problem or if I am doing my transparency wrong

These are the most relevant parts of my code

imgResponse = openai.Image.create_edit(
    image=open(source_path, 'rb'),
    prompt=text_prompt,
    n=1,
    size="1024x1024"
)
def background():
    background = Image.new('RGBA', (1024, 1024), (0, 0, 0, 0))
    return background

def extend(source, direction):
        bg = background()
        if direction == "down":
            bg.paste(source, (0, -56))
            print("Extending downwards")
            return bg

        elif direction == "left":
            ext_left = bg.paste(source, (840, 0))    
            return ext_left

        elif direction == "right":
            ext_right = bg.paste(source, (-840, 0))
            return ext_right

        elif direction == "up":
            ext_up = bg.paste(source, (0, 56))
            return ext_up
    
        return "input not correct"

Full context
main

# Must have imports
import os, sys
import openai

# My own imports
import urllib.request      # For saving the image from the url
import easygui             # For making a simple popup for user input
from PIL import Image

# Import other scripts
import imgGenerator
import imgEdit

# This is the api key that identifies my application
openai.api_key = "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■NO8xap58"
img_path = "tmp/"     # The path that is temporarily save 
                                # the image file

# Setting the prompt for image generation through user-input
text_prompt = easygui.enterbox("Enter the prompt for image generation")

# Creating a 1024x1024 starting image
image_url = imgGenerator.createImage(text_prompt)

# Saving the image from the url to the temporary path
# I defined earlier
urllib.request.urlretrieve(image_url, img_path + "source.png")

### Make a 1080x1080 file with the source image pasted on top of a transparent background
### to make image that I can generate a bottom to

source = Image.open(img_path + "source.png")
extDown = imgEdit.extend(source, "down")
extDown.save(img_path + 'downTr.png', quality=95)

url = imgGenerator.extend(extDown, text_prompt)
urllib.request.urlretrieve(url, img_path + "downTr.png")

Editing functions

from PIL import Image


def background():
    background = Image.new('RGBA', (1024, 1024), (0, 0, 0, 0))
    return background

def extend(source, direction):
        bg = background()
        if direction == "down":
            bg.paste(source, (0, -56))
            print("Extending downwards")
            return bg

        elif direction == "left":
            ext_left = bg.paste(source, (840, 0))    
            return ext_left

        elif direction == "right":
            ext_right = bg.paste(source, (-840, 0))
            return ext_right

        elif direction == "up":
            ext_up = bg.paste(source, (0, 56))
            return ext_up
    
        return "input not correct"

def has_transparency(img):
    if img.info.get("transparency", None) is not None:
        return True
    if img.mode == "P":
        transparent = img.info.get("transparency", -1)
        for _, index in img.getcolors():
            if index == transparent:
                return True
    elif img.mode == "RGBA":
        extrema = img.getextrema()
        if extrema[3][0] < 255:
            return True

    return False

Img generation functions

import os, sys
import openai


def createImage(text_prompt):
    imgResponse = openai.Image.create(
        prompt=text_prompt,
        n=1,
        size="1024x1024"
    )
    image_url = imgResponse['data'][0]['url']
    return image_url

def extend(source, text_prompt):
    imgResponse = openai.Image.create_edit(
        image=open(source),
        prompt=text_prompt,
        n=1,
        size="1024x1024"
    )
    image_url = imgResponse['data'][0]['url']
    return image_url

def has_transparency(img):
    if img.info.get("transparency", None) is not None:
        return True
    if img.mode == "P":
        transparent = img.info.get("transparency", -1)
        for _, index in img.getcolors():
            if index == transparent:
                return True
    elif img.mode == "RGBA":
        extrema = img.getextrema()
        if extrema[3][0] < 255:
            return True

    return False
2 Likes

same problem here, may be mask is not optional?

similar issue here! i am still unable to create edits without having both the image and the mask arguments

same problem… did any of you find out how to solve?

I ended up caving and using a mask.
I think the documentation isn’t accurat and that using a mask isn’t optional.

3 Likes

You could try creating a general mask with only one transparent pixel - but then I suspect it wont change the image. It only changes the masked pixels

Another idea is to create a mask where every pixel is transparent (except 1), and the AI will change the entire image contents. I would also try all pixels transparent - but it may need 1 pixel to analyze.

If it works, you could use the same mask file for all requests in the future

Let us know how you get on if you try this

1 Like

I don’t have my PC here atm, but I might give some of that a try when I get home,
Il keep you updated if I do

2 Likes

I experimented with different masks, trying to find a method to use a single mask for all edits.

Putting a completely transparent mask works, of course, it has to be the same size in pixels as the image you upload.

It just doesn’t work in my case, because it changes the output too much, the ai acts on almost the whole area of the image.

That’s what I thought

Essentially you are giving DALL-E the area it can change by using the mask

So it has to be specific to the image each time

Interesting. It works for me without the mask. I just have to add alpha transparency on the original image where I want to edit :slight_smile:

1 Like

Thank you, Can you post your code snippet here?

1 Like

Hi Guys,

for me it looks like the mask is optional, but I always get the same image as the original one.

feedback1 = "a budgie"
# Generate image
response = openai.Image.create(
  prompt=feedback1,
  n=1,
  size="1024x1024",
  model="image-alpha-001"
)
url = response['data'][0]['url']
tmpImg =Image.open(requests.get(url, stream=True).raw).convert("RGBA")
tmpImg.show()
filename = f"{int(time.time())}.png"
tmpImg.save(filename, "PNG")

feedback2 = "change its color to orange"

response = openai.Image.create_edit(
          image = open(filename,"rb"),
          prompt=feedback2,
          n=1,
          size="1024x1024",
          model="image-alpha-001"
        )
tmpImg = Image.open(requests.get(url, stream=True).raw).convert("RGBA")
tmpImg.show()

This doesn’t change the picture at all

Hey, I saw all problems on Google and this solve my problem as you can use an extra editor which generates mask image and all the details and source code of the editor is here:

[type or paste code here](https://medium.com/@david.richards.tech/how-to-create-openai-dall-e-mask-images-ed8feb562eba)