Skip to content

Commit ff628d1

Browse files
expanded the example to include saving fingerprint image
1 parent 0e3ac3c commit ff628d1

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

examples/fingerprint_simpletest_rpi.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,41 @@ def enroll_finger(location):
156156

157157
return True
158158

159+
def save_fingerprint_image(filename):
160+
"""Scan fingerprint then save image to filename."""
161+
while finger.get_image():
162+
pass
163+
164+
# let PIL take care of the image headers and file structure
165+
from PIL import Image
166+
img = Image.new('L', (256, 288), 'white')
167+
pixeldata = img.load()
168+
mask = 0b00001111
169+
result = finger.get_fpdata(sensorbuffer="image")
170+
171+
# this block "unpacks" the data received from the fingerprint
172+
# module then copies the image data to the image placeholder "img"
173+
# pixel by pixel. please refer to section 4.2.1 of the manual for
174+
# more details. thanks to Bastian Raschke and Danylo Esterman.
175+
# pylint: disable=invalid-name
176+
x = 0
177+
# pylint: disable=invalid-name
178+
y = 0
179+
# pylint: disable=consider-using-enumerate
180+
for i in range(len(result)):
181+
pixeldata[x, y] = (int(result[i]) >> 4) * 17
182+
x += 1
183+
pixeldata[x, y] = (int(result[i]) & mask) * 17
184+
if x == 255:
185+
x = 0
186+
y += 1
187+
else:
188+
x += 1
189+
190+
if not img.save(filename):
191+
return True
192+
return False
193+
159194

160195
##################################################
161196

@@ -185,7 +220,9 @@ def get_num(max_number):
185220
print("e) enroll print")
186221
print("f) find print")
187222
print("d) delete print")
223+
print("s) save fingerprint image")
188224
print("r) reset library")
225+
print("q) quit")
189226
print("----------------")
190227
c = input("> ")
191228

@@ -201,8 +238,16 @@ def get_num(max_number):
201238
print("Deleted!")
202239
else:
203240
print("Failed to delete")
241+
if c == 's':
242+
if save_fingerprint_image("fingerprint.png"):
243+
print("Fingerprint image saved")
244+
else:
245+
print("Failed to save fingerprint image")
204246
if c == 'r':
205247
if finger.empty_library() == adafruit_fingerprint.OK:
206248
print("Library empty!")
207249
else:
208250
print("Failed to empty library")
251+
if c == 'q':
252+
print("Exiting fingerprint example program")
253+
raise SystemExit

0 commit comments

Comments
 (0)