Skip to content

Commit 5be27fb

Browse files
committed
Imports rewriting
1 parent a27180d commit 5be27fb

5 files changed

Lines changed: 76 additions & 79 deletions

File tree

quirc/__init__.py

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,7 @@
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

quirc/api/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
# -*- coding: utf-8 -*-
22

3-
from quirc.api.functions import version, new, resize, destroy, begin, end, count, extract, decode
4-
from quirc.api import constants, functions, structures
5-
6-
__all__ = (
7-
'version', 'new', 'resize', 'destroy', 'begin', 'end', 'count', 'extract', 'decode',
8-
'constants', 'functions', 'structures'
9-
)
3+
import constants, exceptions, structures
4+
from functions import *

quirc/api/exceptions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# -*- coding: utf-8 -*-
22

3-
43
class QuircException(BaseException):
54
pass
65

76

8-
class DecodeException(QuircException):
7+
class DecodeException(QuircException, ValueError):
98

109
def __init__(self, message):
1110
self._message = message

quirc/api/functions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
__all__ = ('version', 'new', 'destroy', 'resize', 'begin', 'end', 'count', 'extract', 'decode')
5+
46
import ctypes
57
from ctypes.util import find_library
68

quirc/base.py

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

0 commit comments

Comments
 (0)