Skip to content

Commit c65aaaf

Browse files
committed
Initial commit
0 parents  commit c65aaaf

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

qr_encode.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <Python.h>
2+
#include <stdlib.h>
3+
#include <qrencode.h>
4+
5+
static PyObject *qr_encode(PyObject *self, PyObject *args)
6+
{
7+
char *str;
8+
int i, version, level, hint, case_sensitive, num_pixels;
9+
QRcode *code;
10+
PyObject *ret;
11+
12+
if(!PyArg_ParseTuple(args, "siiii", &str, &version, &level, &hint,
13+
&case_sensitive))
14+
return NULL;
15+
16+
code = QRcode_encodeString(str, version, level, hint, case_sensitive);
17+
18+
num_pixels = code->width * code->width;
19+
for(i = 0; i < num_pixels; i++)
20+
code->data[i] = 255 - (code->data[i] & 0x1) * 0xFF;
21+
22+
ret = Py_BuildValue("(iis#)", code->version, code->width,
23+
code->data, num_pixels);
24+
QRcode_free(code);
25+
return ret;
26+
};
27+
28+
static PyMethodDef qr_encode_methods[] =
29+
{
30+
{"encode", qr_encode, METH_VARARGS, "Encodes a string as a QR-code. Returns a tuple of (version, width, data)"},
31+
{NULL, NULL, 0, NULL}
32+
};
33+
34+
PyMODINIT_FUNC
35+
initqr_encode(void)
36+
{
37+
PyObject *m = Py_InitModule("qr_encode", qr_encode_methods);
38+
if(m == NULL)
39+
return;
40+
}

qrencode/__init__.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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)

setup.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from distutils.core import setup, Extension
2+
3+
encode = Extension('qr_encode', sources=['qr_encode.c'], libraries=['qrencode'])
4+
5+
setup(name='qrencode',
6+
version='1.0',
7+
description='Encodes QR-codes.',
8+
author='Nick Johnson',
9+
author_email='[email protected]',
10+
url='http://github.com/Arachnid/pyqrencode/tree/master',
11+
long_description='''A simple wrapper for the C qrencode library.''',
12+
packages=['qrencode'],
13+
ext_modules=[encode],
14+
requires=['PIL'])

0 commit comments

Comments
 (0)