How to use image jpg with numpy.load()

I try to implement this code, using colab:

x = np.load('data_sample.npy',allow_pickle=True)
stacked_x = np.concatenate([x,x,x],1)

but my sample was an image in jpg format, so I wasn't able to convert these images to .npy to use it.

actually, I tried this code:

import numpy as np

array = np.asarray('image.JPG')


x = np.load(array,allow_pickle=True)
stacked_x = np.concatenate([x,x,x],1)

but I got: enter image description here

and when I use this code :

    x = np.load(array.tobytes(),allow_pickle=True)

I got:enter image description here

so, any suggestion to solve this, precisely to convert .jpg to .npy?

1 answer

  • answered 2021-02-23 00:55 hpaulj

    Look at what your code does:

    In [57]: np.asarray('image.JPG')
    Out[57]: array('image.JPG', dtype='<U9')
    In [58]: np.asarray('image.JPG').tobytes()
    Out[58]: b'i\x00\x00\x00m\x00\x00\x00a\x00\x00\x00g\x00\x00\x00e\x00\x00\x00.\x00\x00\x00J\x00\x00\x00P\x00\x00\x00G\x00\x00\x00'

    That's just playing with the string. It's not doing anything with a file named "image.JPG". You need to use some sort of image processing package to first load the file, converting it from the compressed format to a 3d array.

    np.load is used to load a file that was created by'file.npy'). It doesn't make sense to give that array variable as the file name. It won't load a jpg file.