Search code examples
iosswiftopenai-api

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


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.


Solution

  • 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."