Search code examples
pythonnumpyimage-processinggoogle-colaboratory

How to cast byte image from google colabs upload to array


So i want to make a function that show a preprocessed version of uploaded images by google.colabs.files.upload() method . But it gives me an error when trying to do some open-cv operations, turns out the image is some byte-type variable. I tried to cast it to numpy array and doesn't work, so what is the correct way to cast it?

P.S its okay if you don't give me explanation i just want the code because the deadline for the task is near


Solution

  • As you want (no explanation)

    
    from google.colab import files
    from io import BytesIO
    from PIL import Image
    import numpy as np
    
    im= files.upload()
    im= Image.open(BytesIO(list(im.values())[0]))
    im= np.array(im)