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

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

本文介绍了如何将整数列表转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SFM5020指纹扫描仪,并且正在使用pysfm库.我有一个读取指纹数据并以列表形式提供长度为10909的模板数据的函数.我想将其转换为图像.你能帮我吗?

I am using a SFM5020 fingerprint scanner and I am using pysfm library. I have a function that reads fingerprint data and gives template data of length 10909 in the form of a list. I want to convert it to an image. Can you please help me on this?

我不知道高度和宽度,我只知道模板数据的长度为10909.这是此类模板数据的一部分:

I don't know the height and width, I just know the length of the template data which is 10909. Here's a section of such template data:

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转换为图像吗?

Can you please help me convert template_data to an image?

推荐答案

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

Here comes an educated guess, which was too long for a comment.

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

From the specifications, the SFM5020 has an image size of 272 x 320. That'd be 87.040 pixels in total. You have 10.909 bytes of data, which is 87.272 bits. So, it seems, the pixel data is stored as binary, i.e. each byte represents eight consecutive pixels.

现在,您还有29个附加字节(87.272位-87.040像素= 232位= 29字节).让我们来看看您的template_data:前28个字节或多或少是零.从字节29开始,有很多字节.那也许是白色"背景.最后看,您有一个零.以前,还有很多白色".因此,很可能舍弃前28个字节和最后一个字节以提取实际的指纹数据.

Now, you have 29 additional bytes (87.272 bits - 87.040 pixels = 232 bits = 29 bytes). Let's have a look on your template_data: The first 28 bytes are more or less zeros. Starting from byte 29, there are a lot of ones. That's maybe "white" background. Looking at the end, you have a single zero. Before, there's also a lot of "white". So, most likely, discard the first 28 bytes and the last byte to extract the actual fingerprint data.

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

With the example given and under the assumption, that data is continuous per row, we can extract two rows:

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)如下所示:

The saved image (via Pillow/PIL regarding your tags) would look like this:

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

I can't tell, if that's the beginning of a proper fingerprint. Maybe, just try the above code on your actual template_data. Therefore, uncomment the two lines given. If the fingerprint looks strange, try fp = bits[0:h*w].reshape((w, h)).T. That'd mean, that fingerprint data is stored continuous per column.

希望有帮助!

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

这篇关于如何将整数列表转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:49