如何将字节数组转换为图片[关闭]

Posted

技术标签:

【中文标题】如何将字节数组转换为图片[关闭]【英文标题】:How to convert byte array to picture [closed] 【发布时间】:2018-02-04 01:16:18 【问题描述】:

如何将我的字节数组转换为图片?我希望它保存为 JPG 或 BMP 格式,而不仅仅是显示为文本或在控制台上。

这是示例数组:

[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 72, 0, 72, 0, 0, 255, 219, 0, 67, 0, 14, 10, 11, 13, 11, 9, 14, 13, 12, 13, 16, 15, 14, 17, 22, 36, 23, 22, 20, 20, 22, 44, 32, 33, 26, 36, 52, 46, 55, 54, 51, 46, 50, 50, 58, 65, 83, 70, 58, 61, 78, 62, 50, 50, 72, 98, 73, 78, 86, 88, 93, 94, 93, 56, 69, 102, 109, 101, 90, 108, 83, 91, 93, 89, 255, 219, 0, 67, 1, 15, 16, 16, 22, 19, 22, 42, 23, 23, 42, 89, 59, 50, 59, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 255, 192, 0, 17, 8, 0, 67, 0, 90, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 27, 0, 0, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 0, 2, 4, 6, 1, 7, 255, 196, 0, 46, 16, 0, 2, 2, 1, 3, 4, 0, 6, 0, 6, 3, 0, 0, 0, 0, 0, 1, 2, 0, 3, 17, 4, 18, 33, 19, 49, 65, 81, 5, 20, 34, 50, 97, 129, 35, 51, 66, 82, 113, 161, 145, 177, 241, 255, 196, 0, 25, 1, 0, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 3, 0, 5, 255, 196, 0, 32, 17, 0, 3, 1, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 17, 3, 33, 49, 4, 18, 65, 50, 81, 97, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 64, 23, 230, 43, 64, 123, 146, 48, 33, 151, 109, 78, 89, 185, 43, 244, 129, 234, 99, 171, 80, 90, 157, 152, 25, 7, 32, 226, 93, 89, 152, 41, 83, 245, 99, 13, 159, 18, 55, 254, 138, 57, 125, 101, 122, 132, 167, 40, 168, 16, 99, 129, 222, 69, 189, 107, 185, 171, 80, 25, 118, 131, 144, 115, 156, 192, 81, 131, 65, 177, 215, 42, 190, 120, 154, 180, 250, 122, 41, 2, 203, 47, 80, 196, 239, 36, 140, 136, 97, 82, 237, 5, 231, 140, 211, 165, 109, 129, 54, 18, 135, 183, 7, 17, 169, 212, 181, 40, 173, 96, 37, 79, 5, 189, 69, 86, 87, 180, 51, 171, 6, 243, 149, 237, 147, 54, 86, 83, 229, 16, 171, 51, 59, 113, 98, 191, 111, 212, 51, 78, 31, 125, 29, 245, 251, 46, 134, 212, 178, 218, 172, 235, 219, 196, 191, 76, 17, 145, 21, 211, 99, 212, 249, 175, 236, 30, 12, 105, 70, 173, 44, 93, 165, 72, 97, 46, 226, 249, 42, 186, 126, 146, 242, 112, 53, 218, 6, 213, 64, 189, 115, 122, 128, 227, 142, 227, 188, 27, 215, 42, 86, 77, 80, 208, 177, 235, 129, 233, 254, 35, 23, 174, 7, 100, 209, 80, 152, 112, 122, 95, 133, 88, 250, 94, 169, 179, 146, 50, 20, 119, 48, 26, 230, 21, 88, 136, 203, 176, 168, 236, 59, 159, 243, 49, 117, 110, 166, 210, 155, 217, 27, 177, 195, 120, 158, 239, 107, 92, 189, 132, 187, 118, 201, 158, 59, 149, 233, 234, 173, 28, 252, 57, 27, 90, 198, 189, 202, 170, 6, 78, 79, 113, 25, 184, 85, 78, 141, 149, 45, 71, 111, 117, 60, 17, 57, 154, 153, 171, 96, 245, 185, 87, 95, 34, 48, 249, 195, 171, 176, 29, 77, 228, 17, 129, 192, 238, 34, 185, 77, 96, 87, 79, 78, 155, 73, 69, 107, 163, 168, 217, 118, 75, 14, 63, 38, 21, 43, 61, 82, 21, 51, 248, 204, 207, 162, 27, 40, 110, 144, 123, 213, 120, 33, 187, 16, 61, 67, 13, 90, 223, 96, 122, 198, 8, 238, 177, 111, 142, 91, 218, 120, 141, 38, 158, 98, 44, 247, 109, 109, 140, 155, 88, 113, 159, 115, 222, 166, 208, 48, 199, 62, 79, 169, 109, 37, 226, 250, 109, 107, 16, 99, 171, 129, 159, 56, 255, 0, 201, 235, 168, 119, 42, 7, 62, 132, 202, 184, 190, 189, 203, 208, 167, 189, 52, 30, 141, 83, 161, 250, 142, 76, 217, 166, 212, 245, 9, 15, 199, 168, 156, 0, 167, 151, 24, 252, 201, 212, 193, 36, 28, 48, 154, 71, 45, 194, 79, 240, 202, 162, 95, 76, 126, 193, 76, 15, 76, 123, 139, 244, 218, 227, 140, 62, 61, 77, 95, 48, 159, 221, 44, 143, 149, 45, 118, 77, 92, 31, 209, 242, 64, 25, 156, 177, 28, 137, 161, 114, 216, 68, 4, 177, 30, 60, 192, 86, 72, 76, 159, 50, 201, 97, 22, 6, 4, 140, 30, 49, 49, 101, 33, 129, 193, 43, 130, 8, 134, 210, 186, 163, 146, 232, 31, 140, 115, 50, 245, 9, 39, 39, 36, 249, 133, 76, 240, 124, 67, 157, 4, 102, 150, 220, 41, 10, 150, 176, 172, 182, 72, 7, 180, 53, 90, 183, 210, 53, 157, 59, 3, 2, 177, 117, 119, 109, 98, 15, 57, 148, 123, 13, 231, 10, 112, 1, 201, 49, 90, 213, 140, 100, 240, 125, 240, 91, 152, 216, 149, 217, 119, 76, 31, 63, 153, 208, 116, 159, 78, 253, 71, 110, 165, 64, 99, 32, 114, 63, 83, 153, 248, 78, 152, 234, 237, 8, 70, 43, 79, 184, 231, 152, 254, 212, 170, 141, 37, 148, 37, 238, 89, 148, 133, 70, 96, 73, 63, 129, 30, 97, 53, 184, 35, 166, 186, 60, 179, 54, 218, 203, 75, 86, 219, 70, 67, 19, 156, 254, 160, 116, 203, 103, 68, 245, 173, 47, 110, 123, 145, 142, 34, 202, 236, 178, 187, 48, 114, 174, 135, 4, 71, 58, 166, 83, 66, 217, 253, 88, 236, 61, 153, 55, 29, 43, 151, 45, 102, 15, 200, 156, 180, 244, 206, 199, 248, 187, 43, 5, 136, 245, 39, 92, 249, 99, 42, 157, 55, 168, 216, 44, 41, 98, 248, 50, 163, 86, 216, 28, 87, 251, 19, 47, 4, 211, 131, 15, 244, 224, 152, 90, 233, 118, 165, 173, 82, 187, 83, 190, 76, 207, 129, 220, 96, 201, 146, 20, 228, 247, 241, 46, 195, 131, 212, 195, 60, 241, 152, 69, 99, 187, 129, 218, 100, 76, 130, 8, 239, 53, 212, 113, 201, 61, 160, 163, 130, 237, 12, 64, 108, 224, 247, 34, 71, 57, 33, 107, 224, 123, 131, 107, 25, 92, 21, 238, 124, 120, 158, 179, 251, 63, 84, 84, 16, 160, 90, 78, 90, 246, 3, 200, 94, 39, 77, 240, 199, 210, 232, 168, 172, 82, 203, 110, 170, 252, 2, 65, 201, 231, 223, 224, 78, 91, 113, 11, 140, 126, 230, 173, 14, 165, 180, 215, 173, 136, 161, 136, 247, 26, 107, 5, 164, 118, 90, 141, 62, 158, 203, 1, 101, 27, 148, 100, 145, 231, 159, 48, 26, 183, 85, 96, 120, 13, 216, 204, 21, 124, 75, 115, 90, 109, 82, 166, 204, 5, 2, 94, 219, 69, 141, 147, 140, 227, 7, 62, 102, 124, 183, 56, 243, 214, 116, 39, 189, 148, 102, 57, 35, 141, 179, 211, 91, 103, 238, 31, 238, 2, 195, 140, 159, 18, 163, 81, 102, 62, 227, 255, 0, 50, 83, 70, 142, 67, 204, 186, 114, 192, 30, 210, 73, 61, 1, 2, 40, 195, 15, 243, 46, 126, 252, 120, 245, 36, 145, 25, 199, 172, 126, 185, 226, 242, 78, 121, 146, 73, 223, 135, 4, 63, 202, 111, 212, 215, 167, 39, 253, 73, 36, 74, 240, 43, 211, 161, 214, 86, 155, 40, 59, 70, 70, 63, 234, 101, 14, 193, 73, 7, 28, 201, 36, 199, 155, 249, 6, 60, 51, 218, 236, 119, 101, 143, 136, 61, 199, 220, 146, 64, 135, 71, 255, 217]

【问题讨论】:

您好,欢迎来到 Stack Overflow。由于您当前的问题,它不符合您在提问之前应该阅读的How to Ask 页面上的指南。堆栈溢出不是您要求人们为您编码的服务;相反,您应该自己尝试解决问题,当您遇到特定错误或问题时,您可以提出特定问题。由于您似乎不知道如何开始,我会推荐任何 Python 教程的第一页,并让您在尝试制作图片之前学习如何将单词打印到控制台。 你有示例代码吗? SO 将帮助您解决问题,而不是解决它! 我现在不知道如何将字节数组转换为图像。 :// 也许您可以在这里找到一些帮助(以及如何正确提问的示例):PIL: Convert Bytearray to Image 前 4 个字节是 jpeg 图像 (255, 216, 255, 224 == FF D8 FF E0)magic number。 【参考方案1】:

每个 MIME 类型都有一个签名(幻数)。通过第一个字节,它是一个 JPEG img。

# your array
arr = [255, 216, 255, 224, 0, ...]
>>> bytearray(arr[:4])
bytearray(b'\xff\xd8\xff\xe0')

FF D8 FF E0 - 这是jpeg 签名图片。

我试过了:

f = open('/tmp/myimage.jpeg', 'wb')
f.write(bytearray(arr))
f.close()

然后得到下一张图片:

【讨论】:

以上是关于如何将字节数组转换为图片[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何将base64位的字节数组转换成图片并显示

将 3 维字节数组转换为单字节数组 [关闭]

将html转换为字节数组java中的图像

在Java中将字节[]写入文件[关闭]

在java中如何把字节数组存储到数据库?

如何将字节数组的字符串转换为字节数组