我有用Python编写的代码,该代码使用ProcessMessagesAsync
库读取图像文件,并将其转换为PIL
值的n维numpy数组:
uint8
一旦我尝试打印出从 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)
到image[0][0][0]
的像素值,我将得到以下输出:
image[9][0][0]
在Go中,我正在通过调用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
函数读取图像,并通过为Go调用Tensorflow API将其转换为Tensor,如下所示:
ioutil.ReadFile
一旦我通过b, err := ioutil.ReadFile(imagePath)
if err != nil {
return nil, err
}
tensor, err := tf.NewTensor(string(b))
if err != nil {
return nil, err
}
运行此张量,就可以将其值转换为tf.Session
,扩展其尺寸,使其与模型输入匹配,并尝试从{{1}打印出像素值}到uint8
为止,我得到以下输出:
tensor[0][0][0][0]
我认为使用tensor[0][9][0][0]
库在python中读取Image时,它会在内部将字节数组解码为特定格式的图像。在Go中,我正在读取字节数组并将其按原样转换为Tensor。没有解码阶段。
我无法在Python中更改代码,这意味着我必须找到一种方法来解码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
函数读取的再见数组,就像PIL
对其进行解码一样。
Go中有一个软件包,可以让我像PIL一样读取图像。解决此类问题的最佳方法是什么?可能找出ReadFile
库是否包装了某个C函数并在Go中创建了相同的包装函数?