Skip to content

Commit

Permalink
[#117] Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
quicklizard99 committed Oct 20, 2021
2 parents 38e932b + fbbb558 commit 6517f16
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 47 deletions.
6 changes: 4 additions & 2 deletions .github/worflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ jobs:
pip install -r requirements-ci.txt
- name: Run tests
run: |
pytest --verbose --cov=pyzbar yzbar
run: pytest --verbose --cov=pyzbar yzbar

- name: Upload coverage
run: coveralls
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: python

sudo: required

dist: xenial

python:
- "2.7"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "3.10"

before_install:
- sudo apt-get -qq update
- sudo apt-get install -y libzbar0 python-opencv

install:
- pip install tox-travis coveralls

script:
- tox

after_success:
- coveralls
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
### v0.1.9

* #108 Python 3.8., 3.9 and 3.10; drop support for Python 3.4
* #113 Add support for images loaded using imageio
* #108 Python 3.8, 3.9 and 3.10; drop support for Python 3.4
* #107 Support SQ Codes; tolerate unrecognised codes
* #86 Read barcodes containing null characters
* #84 Barcode orientation
* #46 Include quality

### v0.1.8

Expand Down
52 changes: 42 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Read one-dimensional barcodes and QR codes from Python 2 and 3 using the
`zbar <http:https://zbar.sourceforge.net/>`__ library.

- Pure python
- Works with PIL / Pillow images, OpenCV / numpy ``ndarray``\ s, and raw bytes
- Works with PIL / Pillow images, OpenCV / imageio / numpy ``ndarray``\ s, and raw bytes
- Decodes locations of barcodes
- No dependencies, other than the zbar library itself
- Tested on Python 2.7, and Python 3.5 to 3.10
Expand Down Expand Up @@ -71,15 +71,19 @@ The ``decode`` function accepts instances of ``PIL.Image``.
polygon=[
Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
Point(x=361, y=550)
]
],
orientation="UP",
quality=77
)
Decoded(
data=b'Rana temporaria', type='CODE128',
rect=Rect(left=4, top=0, width=390, height=76),
polygon=[
Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
Point(x=394, y=0)
]
],
orientation="UP",
quality=77
)
]

Expand All @@ -97,15 +101,19 @@ images using `OpenCV <http:https://opencv.org/>`__.
polygon=[
Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
Point(x=361, y=550)
]
],
orientation="UP",
quality=77
)
Decoded(
data=b'Rana temporaria', type='CODE128',
rect=Rect(left=4, top=0, width=390, height=76),
polygon=[
Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
Point(x=394, y=0)
]
],
orientation="UP",
quality=77
)
]

Expand All @@ -126,15 +134,19 @@ is eight bits-per-pixel.
polygon=[
Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
Point(x=361, y=550)
]
],
orientation="UP",
quality=77
)
Decoded(
data=b'Rana temporaria', type='CODE128',
rect=Rect(left=4, top=0, width=390, height=76),
polygon=[
Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
Point(x=394, y=0)
]
],
orientation="UP",
quality=77
)
]

Expand All @@ -148,15 +160,19 @@ is eight bits-per-pixel.
polygon=[
Point(x=37, y=551), Point(x=37, y=625), Point(x=361, y=626),
Point(x=361, y=550)
]
],
orientation="UP",
quality=77
)
Decoded(
data=b'Rana temporaria', type='CODE128',
rect=Rect(left=4, top=0, width=390, height=76),
polygon=[
Point(x=4, y=1), Point(x=4, y=75), Point(x=394, y=76),
Point(x=394, y=0)
]
],
orientation="UP",
quality=77
)
]

Expand All @@ -183,7 +199,9 @@ symbol types
polygon=[
Point(x=27, y=27), Point(x=27, y=172), Point(x=172, y=172),
Point(x=172, y=27)
]
],
orientation="UP",
quality=1
)
]

Expand All @@ -192,6 +210,17 @@ symbol types
>>> decode(Image.open('pyzbar/tests/qrcode.png'), symbols=[ZBarSymbol.CODE128])
[]

Quality field
-------------
From
`zbar.h <https://sourceforge.net/p/zbar/code/ci/default/tree/include/zbar.h#l359>`__, the quality field is

...an unscaled, relative quantity: larger values are better than smaller
values, where "large" and "small" are application dependent. Expect the exact
definition of this quantity to change as the metric is refined. currently,
only the ordered relationship between two values is defined and will remain
stable in the future

Bounding boxes and polygons
---------------------------

Expand All @@ -216,6 +245,9 @@ Contributors
------------

- Alex (@globophobe) - first implementation of barcode locations
- Dmytro Ferens (@dferens) - barcode orientation
- Ismail Bento (@isman7) - support for images loaded using imageio
- @jaant - read barcodes containing null characters

License
-------
Expand Down
43 changes: 31 additions & 12 deletions pyzbar/pyzbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
zbar_image_scanner_create, zbar_image_scanner_destroy,
zbar_image_create, zbar_image_destroy, zbar_image_set_format,
zbar_image_set_size, zbar_image_set_data, zbar_scan_image,
zbar_image_first_symbol, zbar_symbol_get_data,
zbar_image_first_symbol, zbar_symbol_get_data_length,
zbar_symbol_get_data, zbar_symbol_get_orientation,
zbar_symbol_get_loc_size, zbar_symbol_get_loc_x, zbar_symbol_get_loc_y,
zbar_symbol_next, ZBarConfig, ZBarSymbol, EXTERNAL_DEPENDENCIES
zbar_symbol_get_quality, zbar_symbol_next, ZBarConfig, ZBarOrientation,
ZBarSymbol, EXTERNAL_DEPENDENCIES,
)

__all__ = [
'decode', 'Point', 'Rect', 'Decoded', 'ZBarSymbol', 'EXTERNAL_DEPENDENCIES'
]


Decoded = namedtuple('Decoded', ['data', 'type', 'rect', 'polygon'])
Decoded = namedtuple('Decoded', 'data type rect polygon quality orientation')

# ZBar's magic 'fourcc' numbers that represent image formats
_FOURCC = {
Expand Down Expand Up @@ -97,22 +99,35 @@ def _decode_symbols(symbols):
Decoded: decoded symbol
"""
for symbol in symbols:
data = string_at(zbar_symbol_get_data(symbol))
# The 'type' int in a value in the ZBarSymbol enumeration
symbol_type = ZBarSymbol(symbol.contents.type).name
data = string_at(
zbar_symbol_get_data(symbol),
zbar_symbol_get_data_length(symbol)
)
# The 'type' int should be a value in the ZBarSymbol enumeration
try:
symbol_type = ZBarSymbol(symbol.contents.type)
except ValueError:
# This release of zbar supports a type that pyzbar does not know about
symbol_type = "Unrecognised type [{0}]".format(symbol.contents.type)
else:
symbol_type = symbol_type.name

quality = zbar_symbol_get_quality(symbol)
polygon = convex_hull(
(
zbar_symbol_get_loc_x(symbol, index),
zbar_symbol_get_loc_y(symbol, index)
)
for index in _RANGEFN(zbar_symbol_get_loc_size(symbol))
)

orientation = ZBarOrientation(zbar_symbol_get_orientation(symbol)).name
yield Decoded(
data=data,
type=symbol_type,
rect=bounding_box(polygon),
polygon=polygon
polygon=polygon,
orientation=orientation,
quality=quality,
)


Expand All @@ -122,14 +137,18 @@ def _pixel_data(image):
Returns:
:obj: `tuple` (pixels, width, height)
"""
# Test for PIL.Image and numpy.ndarray without requiring that cv2 or PIL
# are installed.
if 'PIL.' in str(type(image)):
# Test for PIL.Image, numpy.ndarray, and imageio.core.util without
# requiring that cv2, PIL, or imageio are installed.

image_type = str(type(image))
if 'PIL.' in image_type:
if 'L' != image.mode:
image = image.convert('L')
pixels = image.tobytes()
width, height = image.size
elif 'numpy.ndarray' in str(type(image)):
elif 'numpy.ndarray' in image_type or 'imageio.core.util' in image_type:
# Different versions of imageio use a subclass of numpy.ndarray
# called either imageio.core.util.Image or imageio.core.util.Array.
if 3 == len(image.shape):
# Take just the first channel
image = image[:, :, 0]
Expand Down
Binary file added pyzbar/tests/code128_null_character.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6517f16

Please sign in to comment.