简体   繁体   English

将uint8数组转换为图像

[英]Converting uint8 array to image

I am trying to convert a uint8 array of a 48x48 image to its corresponding 48x48 image file (jpg/jpeg/gif). 我正在尝试将48x48图像的uint8 array转换为其相应的48x48图像文件(jpg / jpeg / gif)。 I tried converting the array contents to first binary and then wrote ('wb' mode) it to a file, but that did not work out. 我尝试将数组内容转换为第一个二进制文件,然后将其写入(“ wb”模式)到文件中,但是没有成功。

Is there a way I can accomplish this? 有没有办法可以做到这一点?

If you are producing the image in TensorFlow (as I'm inferring from your tag), you can use the tf.image.encode_jpeg() or tf.image.encode_png() ops to encode a uint8 tensor as an image: 如果您正在TensorFlow中生成图像(根据我的标签推断),则可以使用tf.image.encode_jpeg()tf.image.encode_png()操作将uint8张量编码为图像:

uint8_data = ...
image_data = tf.image.encode_png(uint8_data)

The result of either op is a tf.string tensor that you can evaluate and write out to a file. 任一个op的结果都是一个tf.string张量,您可以评估它并将其写出到文件中。

I was able to the same very easily using Octave. 使用Octave,我能够非常轻松地完成相同的操作。 Here i have tried to generate a random matrix of 48x48 and then i saved the image as jpg format. 在这里,我尝试生成48x48的随机矩阵,然后将图像另存为jpg格式。

img = rand(48,48);
imwrite(img, "test.jpg")

48x48的图片

You can save any type of image with this approach. 您可以使用这种方法保存任何类型的图像。

If you can give some more details about what you want to achieve. 如果您可以提供有关您要实现的目标的更多详细信息。 Do u need to do it just once or u need it as part of program. 您是否只需要执行一次,或者是否需要将其作为程序的一部分。

Hope that helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM