Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire Uploads PNG to Flask with White Background

I am trying to implement a Flask backend endpoint where two images can be uploaded, a background and a foreground (the foreground has a transparent background), and it will paste the foreground on top of the background. I have the following Python code that I have tested with local files and works:

background_name = request.args.get("background")
overlay_name = request.args.get("overlay")
output_name = request.args.get("output")

background_data = request.files["background"].read()
overlay_data = request.files["overlay"].read()

background = Image.open(BytesIO(background_data))
overlay = Image.open(BytesIO(overlay_data))

background = background.convert("RGBA")
overlay = overlay.convert("RGBA")

overlay = resize_image(overlay, background.size)
background.paste(overlay, (0, 0, background.size[0], background.size[1],), overlay)
background.save(output_name, "PNG")

However, when I try to upload the same images through Alamofire and the following code:

Alamofire.upload(multipartFormData: { formData in
        formData.append(bgData, withName: "background", fileName: "background.jpg", mimeType: "image/jpeg")
        formData.append(fgData, withName: "foreground", fileName: "foreground.png", mimeType: "image/png")
    }, to: "http://localhost:8080/image_overlay?background=background%2Ejpg&overlay=overlay%2Epng&output=result%2Epng", encodingCompletion: { result in
        switch result {
        case .success(let upload, _, _):
            upload.validate().responseJSON(completionHandler: { response in
                switch response.result {
                case .success(let value): print("success: \(value)")
                case .failure((let error)): print("response error \(error)")
                }
            })
        case .failure(let error):
            print("encoding error \(error)")
        }
    })

The foreground appears with a white background instead of a transparent background and the resulting image is just the foreground with a white background. How can I get Alamofire to send the transparency?

EDIT: I tried translating this to a cURL request and it works as expected. I used nc-l localhost 8080 to view the full request, and it seems that with the foreground picture, even though the Content-Type was set to "application/octet-stream", the next line was "?PNG". This line was no present from the Alamofire request. How can I get the request to recognize the image as a PNG?

like image 718
genghiskhan Avatar asked Jul 04 '17 15:07

genghiskhan


1 Answers

I can't believe I was doing this, but when I was obtaining to image data, I was using

let fgData = UIImageJPEGRepresentation(UIImage(named: "overlay.png"), 0.75)

This was the only way I had ever obtained data from an image in Swift, even though obviously it should be what I changed it to:

let fgData = UIImagePNGRepresentation(UIImage(named: "overlay.png"))
like image 97
genghiskhan Avatar answered Oct 22 '22 14:10

genghiskhan