如何将整数列表转换为图像?

How to convert list of integers to image?

我正在使用 SFM5020 指纹扫描仪,并且正在使用 pysfm 库。我有一个读取指纹数据并以列表形式给出长度为 10909 的模板数据的功能。我想将其转换为图像。你能帮我解决这个问题吗?

我不知道高和宽,我只知道模板数据的长度是10909,下面是这样一段模板数据:

template_data = [16, 1, 0, 0, 64, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 84, 1, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 15, 255, 63, 240, 199, 127, 255, 23, 255, 255, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 31, 249, 255, 255, 255, 255, 227, 127, 224, 15, 254, 248, 7, 254, 247, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 ,.................................. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0]

你能帮我把template_data转换成图片吗?

这是一个有根据的猜测,对于评论来说太长了。

specifications开始,SFM5020的图像大小为272 x 320。总共是 87.040 像素。您有 10.909 字节的数据,即 87.272 位。所以,看起来,像素数据是以二进制形式存储的,即每个字节代表八个连续的像素。

现在,您有 29 个额外字节(87.272 位 - 87.040 像素 = 232 位 = 29 字节)。让我们看看你的 template_data:前 28 个字节或多或少是零。从第29字节开始,有很多。这可能是 "white" 背景。看看最后,你有一个零。之前,也有很多"white"。因此,很可能会丢弃前 28 个字节和最后一个字节以提取实际的指纹数据。

根据给出的示例并假设每行数据是连续的,我们可以提取两行:

import numpy as np
from PIL import Image

# Data
head = [16, 1, 0, 0, 64, 1, 0, 0,                   # Byte 0 - 7
        0, 0, 0, 0, 0, 0, 0, 0,                     # Byte 8 - 15
        1, 0, 0, 0, 0, 84, 1, 0,                    # Byte 16 - 23
        0, 0, 0, 0, 255, 255, 255, 255,             # Byte 24 - 31
        255, 255, 255, 255, 255, 255, 255, 255,     # ...
        15, 255, 63, 240, 199, 127, 255, 23,
        255, 255, 31, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 31, 249, 255,
        255, 255, 255, 227, 127, 224, 15, 254,
        248, 7, 254, 247, 31, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255,
        255, 255]
# ... Rest of the data...
tail = [255, 255, 255, 255, 255, 255, 255, 255,     # Byte 10896 - 10903
        255, 255, 255, 255, 0]                      # Byte 10904 - 10908

# Unpack bits from bytes starting from byte 28
bits = np.unpackbits(np.array(head[28:len(head)]).astype(np.uint8)) * 255
#bits = np.unpackbits(np.array(template_data[28:-1]).astype(np.uint8)) * 255

# SFM5020 has image size of 272 x 320
# https://www.supremainc.com/embedded-modules/en/modules/sfm-5000.asp
w = 272
h = 320

# Extract fingerprint data from bits
fp = bits[0:2*w].reshape((2, w))
# fp = bits[0:h*w].reshape((h, w))

# Save fingerprint as image via Pillow/PIL
fp_pil = Image.fromarray(fp, 'L')
fp_pil.save('fp.png')

保存的图像(通过 Pillow/PIL 关于您的标签)将如下所示:

我不知道,这是否是正确指纹的开始。也许,只需在您的 template_data 上尝试上面的代码。因此,取消注释给定的两行。如果指纹看起来很奇怪,请尝试 fp = bits[0:h*w].reshape((w, h)).T。这意味着,指纹数据每列连续存储。

希望对您有所帮助!

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.1
NumPy:       1.18.1
Pillow:      7.0.0
----------------------------------------