changeset 2:ad577744dd8e

Drop dependency on numpy
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 24 Feb 2015 17:23:39 +0100
parents ee040846c98f
children 1311f6533978
files src/bnpparibas.py src/keypad.npy src/keypad.png
diffstat 3 files changed, 13 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/bnpparibas.py	Tue Feb 24 19:30:26 2015 +0100
+++ b/src/bnpparibas.py	Tue Feb 24 17:23:39 2015 +0100
@@ -17,7 +17,6 @@
 from urllib.parse import urljoin
 
 import bs4
-import numpy as np
 import requests
 
 from PIL import Image
@@ -93,24 +92,26 @@
         
         
 def imslice(image):
-    for x, y in product(range(0, 5), range(0, 5)):
-        islice = image[27*x+1:27*(x+1), 27*y+1:27*(y+1), 0]
-        yield islice
+    for y, x in product(range(0, 5), range(0, 5)):
+        yield image.crop((27 * x + 1, 27 * y + 1, 27 * (x + 1), 27 * (y + 1)))
 
 
 def imdecode(image):
-    keypad = np.load(os.path.join(os.path.dirname(__file__), 'keypad.npy'))
+    # load reference keypad
+    keypad = Image.open(os.path.join(os.path.dirname(__file__), 'keypad.png')).convert('L')
+    keypad = [ keypad.crop((26 * i, 0, 26 * (i + 1), 26)) for i in range(10) ]
     immap = {}
-    for n, islice in enumerate(imslice(image)):
-        # skip empty tiles
-        if np.mean(islice) > 248.0:
+    for n, tile in enumerate(imslice(image)):
+        # skip tiles with background only
+        if tile.getextrema()[0] > 0:
             continue
         # compare to reference tiles
         for d in range(0, 10):
-            delta = np.sum(islice - keypad[d])
-            if delta < 100:
-                print(delta)
+            if tile == keypad[d]:
                 immap[d] = n + 1
+                break
+    if sorted(immap.keys()) != list(range(10)):
+        raise ValueError('keypad decode failed')    
     return immap
 
 
@@ -155,7 +156,7 @@
                 break
         # download keyboard image
         r = self.req.get(urljoin(self.url, src))
-        image = np.array(Image.open(BytesIO(r.content)).convert('RGB'))
+        image = Image.open(BytesIO(r.content)).convert('L')
         # decode digits position
         passwdmap = imdecode(image)
 
Binary file src/keypad.npy has changed
Binary file src/keypad.png has changed