从字节数组转换为图像 [英] Convert from byte array to Image

查看:145
本文介绍了从字节数组转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据长度为498,我想将其转换为图像.数据(字节数组)为:

I have data of length 498, and I want to convert it to an image. The data (byte array) is:

ba = [4, 41, 80, 3, 41, 130, 134, 225, 196, 184, 63, 255, 137, 50, 182, 95, 3, 86, 250, 7, 218, 114, 246, 93, 132, 101, 56, 224, 233, 234, 185, 149, 132, 245, 245, 239, 1, 171, 245, 214, 66, 198, 133, 254, 18, 19, 247, 92, 72, 151, 118, 152, 21, 131, 248, 128, 74, 103, 172, 72, 37, 163, 233, 146, 196, 54, 244, 15, 85, 91, 229, 89, 195, 22, 64, 200, 73, 131, 246, 215, 133, 166, 137, 237, 85, 27, 246, 85, 68, 70, 122, 208, 134, 179, 246, 77, 199, 119, 246, 184, 145, 147, 247, 73, 143, 217, 240, 152, 149, 3, 56, 8, 183, 58, 13, 10, 181, 131, 248, 29, 80, 8, 1, 1, 217, 131, 248, 32, 11, 248, 250, 232, 245, 99, 245, 83, 196, 22, 192, 183, 21, 156, 246, 60, 74, 104, 106, 209, 14, 212, 233, 155, 195, 248, 247, 231, 21, 236, 245, 66, 136, 87, 102, 201, 33, 92, 233, 149, 1, 200, 185, 224, 50, 100, 248, 31, 8, 184, 120, 7, 62, 20, 247, 52, 9, 136, 108, 201, 62, 4, 248, 33, 11, 185, 116, 239, 105, 156, 246, 179, 78, 40, 203, 45, 121, 204, 248, 156, 196, 117, 77, 215, 138, 20, 247, 175, 77, 57, 203, 221, 149, 156, 213, 40, 28, 186, 108, 36, 182, 252, 248, 145, 7, 185, 139, 200, 210, 180, 248, 142, 199, 167, 139, 200, 209, 252, 233, 140, 70, 248, 197, 208, 233, 12, 246, 155, 140, 105, 151, 168, 9, 213, 246, 143, 8, 152, 215, 200, 26, 29, 233, 130, 132, 249, 71, 233, 38, 37, 248, 134, 7, 199, 5, 217, 73, 69, 248, 128, 197, 214, 3, 225, 133, 93, 247, 126, 131, 215, 1, 225, 158, 237, 247, 4, 65, 7, 194, 39, 73, 28, 117, 86, 133, 198, 255, 127, 121, 165, 117, 121, 194, 24, 126, 250, 161, 45, 121, 125, 128, 246, 193, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249, 99, 35, 114, 51, 162, 86, 36, 33, 70, 65, 51, 69, 116, 52, 70, 70, 50, 83, 34, 54, 83, 49, 63, 67, 34, 50, 85, 109, 48, 245, 50, 98, 134, 49, 101, 1, 63, 95, 66, 101, 19, 51, 35, 55, 52, 113, 100, 82, 86, 18, 35, 114, 49, 20, 134, 67, 20, 115, 246, 36, 49, 68, 53, 19, 65, 33, 69, 52, 38, 65, 115, 19, 21, 53, 83, 68, 82, 50, 68, 113, 51, 67, 65, 47, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 211, 203]

我尝试使用

Image.frombuffer('L', (498), ba, 'raw', 'L', 0, 1)

但是,这给了一个错误,说大小必须是一个元组",我尝试给出(498,0)(0,498),这没有给出错误,但是在保存图像时出现了一个错误,因为"SystemError:tile无法扩展外部图片".

But, this gave an error saying 'size must be a tuple', I tried giving (498,0) and (0,498), this gave no error, but while saving the image there was an error as 'SystemError: tile cannot extend outside image'.

推荐答案

您可以尝试fromarray函数

import numpy as np
image = Image.fromarray(np.array(ba).reshape(498,1))

这篇关于从字节数组转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆