I have code written in Python which reads an image file using PIL
library, and converts it to n-dimensional numpy array of uint8
values:
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)
image_raw = Image.open(img)
image = load_image_into_numpy_array(image_raw)
Once I try to print out pixel values from image[0][0][0]
up to image[9][0][0]
, I get the following output:
image[0][0][0] = 51
image[1][0][0] = 51
image[2][0][0] = 47
image[3][0][0] = 40
image[4][0][0] = 38
image[5][0][0] = 48
image[6][0][0] = 65
image[7][0][0] = 81
image[8][0][0] = 94
image[9][0][0] = 89
image[10][0][0] = 84
In Go, I am reading an image by calling the ioutil.ReadFile
function, and converting it to Tensor by calling Tensorflow API for Go like so:
b, err := ioutil.ReadFile(imagePath)
if err != nil {
return nil, err
}
tensor, err := tf.NewTensor(string(b))
if err != nil {
return nil, err
}
Once I run this Tensor through a tf.Session
so that I can convert its values to uint8
, expand its dimensions so it matches the models input and try to print out pixel values from tensor[0][0][0][0]
up to tensor[0][9][0][0]
, I get the following output:
tensor[0][0][0][0]: 50
tensor[0][1][0][0]: 48
tensor[0][2][0][0]: 45
tensor[0][3][0][0]: 39
tensor[0][4][0][0]: 37
tensor[0][5][0][0]: 47
tensor[0][6][0][0]: 65
tensor[0][7][0][0]: 80
tensor[0][8][0][0]: 95
tensor[0][9][0][0]: 88
I figured that when reading Image in python using PIL
library, it internally decodes an byte array to an image of specific format. In Go I am reading an array of bytes and converting it to Tensor as is. There is no decoding phase.
I cannot change code in Python, which means that I have to find a way to decode array of byes read by ReadFile
function, same way as PIL.Image.open
decodes it.
Is there a package in Go which allows me to read an image the same way as PIL does. What would be the best way to approach this type of problem? Possibly find out if PIL.Image
library wraps a certain C function and create same wrapper function in Go?