Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module_size parameter to encode function #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pylibdmtx/pylibdmtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def _encoder():
dmtxEncodeDestroy(byref(encoder))


def encode(data, scheme=None, size=None):
def encode(data, scheme=None, size=None, module_size=None):
"""
Encodes `data` in a DataMatrix image.

Expand All @@ -321,6 +321,8 @@ def encode(data, scheme=None, size=None):
If `None`, defaults to 'Ascii'.
size: image dimensions - one of `ENCODING_SIZE_NAMES`, or `None`.
If `None`, defaults to 'ShapeAuto'.
module_size: Module size in pixels, or `None`.
If `None`, defaults to 5

Returns:
Encoded: with properties `(width, height, bpp, pixels)`.
Expand Down Expand Up @@ -352,9 +354,12 @@ def encode(data, scheme=None, size=None):
)
scheme = getattr(DmtxScheme, scheme_name)

module_size = module_size if module_size else 5

with _encoder() as encoder:
dmtxEncodeSetProp(encoder, DmtxProperty.DmtxPropScheme, scheme)
dmtxEncodeSetProp(encoder, DmtxProperty.DmtxPropSizeRequest, size)
dmtxEncodeSetProp(encoder, DmtxProperty.DmtxPropModuleSize, module_size)

if dmtxEncodeDataMatrix(encoder, len(data), cast(data, c_ubyte_p)) == 0:
raise PyLibDMTXError(
Expand Down
6 changes: 5 additions & 1 deletion pylibdmtx/scripts/write_datamatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def main(args=None):
help="Encoding method; default is 'Ascii'",
choices=ENCODING_SCHEME_NAMES
)
parser.add_argument(
'--module_size',
help="Module size in pixels; default is '5'"
)
parser.add_argument(
'-v', '--version', action='version',
version='%(prog)s ' + pylibdmtx.__version__
Expand All @@ -40,7 +44,7 @@ def main(args=None):
from PIL import Image

encoded = encode(
args.data.encode('utf-8'), size=args.size, scheme=args.scheme
args.data.encode('utf-8'), size=args.size, scheme=args.scheme, module_size=args.module_size
)
im = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
im.save(args.file)
Expand Down
9 changes: 9 additions & 0 deletions pylibdmtx/tests/test_pylibdmtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ def test_encode_scheme(self):

self._assert_encoded_data(data, encoded)

def test_encode_module_size(self):
data = b'hello world'
encoded = encode(data, size='36x36', module_size=2)

self.assertEqual(
Encoded(width=92, height=92, bpp=24, pixels=None),
encoded._replace(pixels=None)
)

def test_invalid_scheme(self):
self.assertRaisesRegexp(
PyLibDMTXError,
Expand Down