Search code examples
pythonpython-3.xchaquopy

Saving image file to a directory


I am receiving an image as bytes that I would like to store using specifically with open command due to library restrictions. Therefore I am unable to use libs like opencv2 and PIL

mport numpy as np
import base64
import cv2
import io
from os.path import dirname, join
from com.chaquo.python import Python

def main(data):
    decoded_data = base64.b64decode(data)

    files_dir = str(Python.getPlatform().getApplication().getFilesDir())
    filename = join(dirname(files_dir), 'image.PNG')

    with open(filename, 'rb') as file:
        img = file.write(img)

    return img

What I would simply like to do is save the image file to the current directory that i am in. Currently when i try my code it gives me the following error:

com.chaquo.python.PyException: TypeError: write() argument must be str, not bytes

Which requires me to have a string. I'd like to know what do i need to do to save the image as a .PNG file to the directory


Solution

  • Try :

    out_file = open(filename, 'wb')
    out_file.write(decoded_data)