如何在python3中使用pil将整数列表转换为图像?

问题描述 投票:0回答:1

我正在使用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转换为图像吗?

python-3.x python-imaging-library fingerprint
1个回答
0
投票

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

specifications开始,SFM5020的图像尺寸为272 x 320。总共是87.040像素。您有10.909字节的数据,即87.272位。因此,似乎像素数据是以二进制形式存储的,即每个字节代表八个连续的像素。

现在,您还有29个附加字节(87.272位-87.040像素= 232位= 29字节)。让我们来看看您的template_data:前28个字节或多或少是零。从字节29开始,有很多字节。那也许是“白色”背景。最后看,您有一个零。以前,还有很多“白色”。因此,很可能丢弃前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)看起来像这样:

Output

我不知道这是否是正确指纹的开始。也许,只需在实际的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
----------------------------------------
© www.soinside.com 2019 - 2024. All rights reserved.