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

Python bindings doesn't support binary mode #284

Open
epl opened this issue Jun 1, 2024 · 0 comments
Open

Python bindings doesn't support binary mode #284

epl opened this issue Jun 1, 2024 · 0 comments

Comments

@epl
Copy link

epl commented Jun 1, 2024

The Python bindings of zbar doesn't support output in binary mode.

Reproduction steps

  • See attached QR code which represents the following 16 byte binary data: echo -ne '\x80\x91\xa2\xb3\xc4\xd5\xe6\xf7\x88\x99\xaa\xbb\xcc\xdd\xee\xff' > bin
  • Please see the following Python code:
import sys
from PIL import Image
import zbar

# usage: python3 example.py IMAGE.PNG
pil = Image.open(sys.argv[1]).convert('L')
image = zbar.Image(*pil.size, 'Y800', pil.tobytes())

scanner = zbar.ImageScanner()
scanner.parse_config('qr.binary')
scanner.scan(image)

for symbol in image:
    print(symbol.data)
  • Please run the code with the provided QR code filename as the first parameter.

Expected behaviour

It'll decode the QR code and then print out:

b'\x80\x91\xa2\xb3\xc4\xd5\xe6\xf7\x88\x99\xaa\xbb\xcc\xdd\xee\xff'

Observed Behavior

It'll throw an exception and print out:

Traceback (most recent call last):
  File "/tmp/example.py", line 18, in <module>
    print(symbol.data)
          ^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

Analysis

I suspect the issue is on this line where it forces the symbol data to be an Unicode string (latest commit as of this issue is a549566):
https://github.com/mchehab/zbar/blob/master/python/symbol.c#L105

I'm not sure what's the best suggested fix, but I imagine (at a very high level), it's to provide a means to propagate the binary config into symbol.c and then skip the PyUnicode_FromStringAndSize() call under those circumstances.

Beyond that, I'm not quite sure about the specifics yet. I've never extended Python with C before (though perhaps now is a good time to learn). To be fair, this is open source, so I should contribute a fix. But if you can provide guidance and/or code, then I'd be grateful please. Else, I'll try to learn how to do it and this issue shall be a notice for others.

Issue also related to #64 and #268

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant