comparison src/bnpparibas.py @ 2:ad577744dd8e

Drop dependency on numpy
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 24 Feb 2015 17:23:39 +0100
parents 02ec4a9ab0f0
children 1311f6533978
comparison
equal deleted inserted replaced
1:ee040846c98f 2:ad577744dd8e
15 from io import BytesIO 15 from io import BytesIO
16 from itertools import product, islice 16 from itertools import product, islice
17 from urllib.parse import urljoin 17 from urllib.parse import urljoin
18 18
19 import bs4 19 import bs4
20 import numpy as np
21 import requests 20 import requests
22 21
23 from PIL import Image 22 from PIL import Image
24 23
25 DB = 'bnpparibas.sqlite' 24 DB = 'bnpparibas.sqlite'
91 d['amount'] = '+' + str(d['credit']) 90 d['amount'] = '+' + str(d['credit'])
92 return TRANSACTION.format(**d) 91 return TRANSACTION.format(**d)
93 92
94 93
95 def imslice(image): 94 def imslice(image):
96 for x, y in product(range(0, 5), range(0, 5)): 95 for y, x in product(range(0, 5), range(0, 5)):
97 islice = image[27*x+1:27*(x+1), 27*y+1:27*(y+1), 0] 96 yield image.crop((27 * x + 1, 27 * y + 1, 27 * (x + 1), 27 * (y + 1)))
98 yield islice
99 97
100 98
101 def imdecode(image): 99 def imdecode(image):
102 keypad = np.load(os.path.join(os.path.dirname(__file__), 'keypad.npy')) 100 # load reference keypad
101 keypad = Image.open(os.path.join(os.path.dirname(__file__), 'keypad.png')).convert('L')
102 keypad = [ keypad.crop((26 * i, 0, 26 * (i + 1), 26)) for i in range(10) ]
103 immap = {} 103 immap = {}
104 for n, islice in enumerate(imslice(image)): 104 for n, tile in enumerate(imslice(image)):
105 # skip empty tiles 105 # skip tiles with background only
106 if np.mean(islice) > 248.0: 106 if tile.getextrema()[0] > 0:
107 continue 107 continue
108 # compare to reference tiles 108 # compare to reference tiles
109 for d in range(0, 10): 109 for d in range(0, 10):
110 delta = np.sum(islice - keypad[d]) 110 if tile == keypad[d]:
111 if delta < 100:
112 print(delta)
113 immap[d] = n + 1 111 immap[d] = n + 1
112 break
113 if sorted(immap.keys()) != list(range(10)):
114 raise ValueError('keypad decode failed')
114 return immap 115 return immap
115 116
116 117
117 def amountparse(value): 118 def amountparse(value):
118 # empty 119 # empty
153 if match: 154 if match:
154 src = match.group(1) 155 src = match.group(1)
155 break 156 break
156 # download keyboard image 157 # download keyboard image
157 r = self.req.get(urljoin(self.url, src)) 158 r = self.req.get(urljoin(self.url, src))
158 image = np.array(Image.open(BytesIO(r.content)).convert('RGB')) 159 image = Image.open(BytesIO(r.content)).convert('L')
159 # decode digits position 160 # decode digits position
160 passwdmap = imdecode(image) 161 passwdmap = imdecode(image)
161 162
162 # encode password 163 # encode password
163 passwdenc = ''.join('%02d' % passwdmap[d] for d in map(int, passwd)) 164 passwdenc = ''.join('%02d' % passwdmap[d] for d in map(int, passwd))