-1

I am using the OpenAI "Create Image Edit" API link. My mask image is maskImage.png.

After calling the API, when the response comes, I have got the error:

Invalid input image - format must be in ['RGBA', 'LA', 'L'], got RGB."

How can I change the PNG image format as ['RGBA', 'LA', 'L'] in iOS Swift?

I have found a solution in JavaScript, How to validate an image before sending to DALL E API with front-end JavaScript. But I can not find a solution in iOS.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Symon
  • 633
  • 6
  • 15

1 Answers1

1

All you have to do is add opacity/alpha to the UIImage/pngData.

extension UIImage {
    var hasAlpha: Bool {
        guard let alphaInfo = self.cgImage?.alphaInfo else {return false}
        return alphaInfo != CGImageAlphaInfo.none &&
            alphaInfo != CGImageAlphaInfo.noneSkipFirst &&
            alphaInfo != CGImageAlphaInfo.noneSkipLast
    }
}

Is a quick way to check if a UIImage has alpha.

You can add alpha with

extension UIImage {

    func imageWithAlpha(alpha: CGFloat) throws -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        draw(at: CGPointZero, blendMode: .normal, alpha: alpha)
        guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else{
            UIGraphicsEndImageContext()
            throw ImageError.unableToAddAplhaToImage
        }
        UIGraphicsEndImageContext()
        return newImage
    }
    enum ImageError: LocalizedError{
        case unableToAddAplhaToImage
    }
}

1 being a dark image and 0 being a transparent image, OpenAI is looking for 0.

The method above will change the entire UIImage, if you use this the entire image will be replaced.

A mask will only have sections of an image that have a value of 0.

The PNG you are showing doesn't show any pixels as zero.

In short, with the code above if image.hasAlpha == false you will get the error you are talking about when you submit.

if if image.hasAlpha == true then it is ok with OpenAI.

In javascript you can use Jimp to simplify the whole thing, you can do all the checks in just a few lines.

Dall E API error: "Invalid input image - format must be in ['RGBA', 'LA', 'L'], got RGB."

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48
  • thank you for your answer. Mask image should be transparent, otherwise got that error. – Symon Apr 20 '23 at 08:12
  • @symon the question is a bit open ended and with swift usually you can post some code so people can reproduce the OpenAI API is not designed to be used in the iOS side (it shouldn’t be). I have this code because I check everything before touching my server. When I first implemented it I spent a good 3 days getting it all connected since Node.js isn’t my strong area. This was one of the errors I was getting. – lorem ipsum Apr 20 '23 at 10:42