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