33
44"""Bindings to QR code decoding library `quirc`"""
55
6- import ctypes
7-
8- from quirc import api
9-
10- USING_PIL = True
11- try :
12- from PIL import Image
13- except ImportError :
14- try :
15- import Image
16- except ImportError :
17- USING_PIL = False
18-
196__version__ = '0.6.1'
20- __all__ = ('api' , 'decode' )
21-
22-
23- def decode (image ):
24- """Recognize image and return generator with all the available QR codes
25-
26- Currently supports only PIL Image object as an parameter
27- """
28-
29- if not (USING_PIL and isinstance (image , Image .Image )):
30- raise TypeError ('Unknown image object type: %s' % type (image ))
31-
32- # Convert to grayscale mode
33- if image .mode not in ('1' , 'L' ):
34- image = image .convert ('L' )
35-
36- width , height = image .size
37- pixels = image .load ()
38-
39- obj = api .new ()
40- api .resize (obj , width , height )
41- buffer = api .begin (obj , width , height )
42-
43- # Fill buffer with a image pixels. One cell, one pixel.
44- # TODO: looks like a very slow operation
45- idx = 0
46- for i in range (width ):
47- for j in range (height ):
48- buffer [idx ] = ctypes .c_uint8 (pixels [j , i ])
49- idx += 1
50-
51- del idx
52-
53- # Finish codes identification
54- api .end (obj )
55-
56- num_codes = api .count (obj )
57-
58- code = api .structures .Code ()
59- data = api .structures .Data ()
60-
61- for i in range (num_codes ):
62-
63- # Extract first code
64- api .extract (obj , i , code )
65- api .decode (code , data )
66-
67- yield {
68- 'corners' : tuple ([(corner .x , corner .y ) for corner in code .corners ]),
69- 'size' : code .size ,
70- 'version' : data .version ,
71- 'ecc_level' : data .ecc_level ,
72- 'mask' : data .mask ,
73- 'data_type' : data .data_type ,
74- 'text' : ctypes .string_at (data .payload , data .payload_len ),
75- }
767
77- api .destroy (obj )
8+ import api
9+ from base import decode
0 commit comments