|
| 1 | +from qr_encode import encode as _encode |
| 2 | +import Image |
| 3 | + |
| 4 | + |
| 5 | +QR_ECLEVEL_L = 0 |
| 6 | +QR_ECLEVEL_M = 1 |
| 7 | +QR_ECLEVEL_Q = 2 |
| 8 | +QR_ECLEVEL_H = 3 |
| 9 | +levels = [QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_Q, QR_ECLEVEL_H] |
| 10 | + |
| 11 | + |
| 12 | +QR_MODE_8 = 2 |
| 13 | +QR_MODE_KANJI = 3 |
| 14 | +hints = [QR_MODE_8, QR_MODE_KANJI] |
| 15 | + |
| 16 | + |
| 17 | +def encode(data, version=0, level=QR_ECLEVEL_L, hint=QR_MODE_8, |
| 18 | + case_sensitive=True): |
| 19 | + """Creates a QR-Code from string data. |
| 20 | + |
| 21 | + Args: |
| 22 | + data: string: The data to encode in a QR-code. If a unicode string is |
| 23 | + supplied, it will be encoded in UTF-8. |
| 24 | + version: int: The minimum version to use. If set to 0, the library picks |
| 25 | + the smallest version that the data fits in. |
| 26 | + level: int: Error correction level. Defaults to 'L'. |
| 27 | + hint: int: The type of data to encode. Either QR_MODE_8 or QR_MODE_KANJI. |
| 28 | + case_sensitive: bool: Should string data be encoded case-preserving? |
| 29 | + Returns: |
| 30 | + A (version, size, image) tuple, where image is a size*size PIL image of |
| 31 | + the QR-code. |
| 32 | + """ |
| 33 | + if isinstance(data, unicode): |
| 34 | + data = data.encode('utf8') |
| 35 | + elif not isinstance(data, basestring): |
| 36 | + raise ValueError('data argument must be a string.') |
| 37 | + version = int(version) |
| 38 | + if level not in levels: |
| 39 | + raise ValueError('Invalid error-correction level.') |
| 40 | + if hint not in hints: |
| 41 | + raise ValueError('Invalid encoding mode.') |
| 42 | + if case_sensitive: |
| 43 | + version, size, data = _encode(data, version, level, hint, True) |
| 44 | + else: |
| 45 | + version, size, data = _encode(data, version, level, hint, False) |
| 46 | + |
| 47 | + im = Image.fromstring('L', (size, size), data) |
| 48 | + return (version, size, im) |
| 49 | + |
| 50 | +def encode_scaled(data, size, version=0, level=QR_ECLEVEL_L, hint=QR_MODE_8, |
| 51 | + case_sensitive=True): |
| 52 | + """Creates a QR-code from string data, resized to the specified dimensions. |
| 53 | +
|
| 54 | + Args: |
| 55 | + data: string: The data to encode in a QR-code. If a unicode string is |
| 56 | + supplied, it will be encoded in UTF-8. |
| 57 | + size: int: Output size. If this is not an exact multiple of the QR-code's |
| 58 | + dimensions, padding will be added. If this is smaller than the |
| 59 | + QR-code's dimensions, it is ignored. |
| 60 | + version: int: The minimum version to use. If set to 0, the library picks |
| 61 | + the smallest version that the data fits in. |
| 62 | + level: int: Error correction level. Defaults to 'L'. |
| 63 | + hint: int: The type of data to encode. Either QR_MODE_8 or QR_MODE_KANJI. |
| 64 | + case_sensitive: bool: Should string data be encoded case-preserving? |
| 65 | + Returns: |
| 66 | + A (version, size, image) tuple, where image is a size*size PIL image of |
| 67 | + the QR-code. |
| 68 | + """ |
| 69 | + version, src_size, im = encode(data, version, level, hint, case_sensitive) |
| 70 | + if size < src_size: |
| 71 | + size = src_size |
| 72 | + qr_size = (size / src_size) * src_size |
| 73 | + im = im.resize((qr_size, qr_size), Image.NEAREST) |
| 74 | + pad = (size - qr_size) / 2 |
| 75 | + ret = Image.new("L", (size, size), 255) |
| 76 | + ret.paste(im, (pad, pad)) |
| 77 | + |
| 78 | + return (version, size, ret) |
0 commit comments