'PNG File Not Successfully Copying Over From Android Studio To Python

I am creating an app using Kotlin and Android Studio. With this app, the user can snap a photo with the android camera and I am trying to send that photo to the backend where I will process it using python. The part in Kotlin where I am creating and sending the png file looks like:

val con = requireActivity().contentResolver
bitmap = MediaStore.Images.Media.getBitmap(con, image_uri)

val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
val byteArray: ByteArray = stream.toByteArray()
val requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), byteArray)

    submitLocation(
        user,
        locationName.text.toString(),
        address.text.toString(),
        requestBody
    )

I can tell that something is making its way to the Python backend but I can not seem to do anything to actually get a legit Png opened up. The python code looks like:

form = request.form
if 'image' in form.keys():
    imgdata = bytearray(str(form['image']).encode())

    f = open('myimage.PNG', 'wb')
    f.write(imgdata)
    f.close()

Something I am noticing is the array in Kotlin is often near 50,000 elements whereas whenever I get it into Python it is usually somewhere just below 30,000 elements. If I run the code show above it creates a file named 'myimage.png' but if I try to open it on my Mac, I get a warning saying:

"The file "myimage.png" could not be opened. It may be damaged or use a file format that Preview doesn’t recognize."


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source