Skip to content

Commit 901f510

Browse files
committed
Fix linter
1 parent 11fb3ac commit 901f510

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ recursive-include docs *.rst
1313
recursive-include labelbox *.py
1414
include labelbox/exporters/pascal_voc_writer/templates/annotation.xml
1515
recursive-include tests *.json
16+
recursive-include tests *.lbx
1617
recursive-include tests *.png
1718
recursive-include tests *.py

labelbox/lbx.py

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,23 @@
3636
_HEADER_LENGTH = 6 * 4
3737

3838

39-
def encode(image):
40-
"""Converts a RGB `PIL.Image` to a `io.BytesIO` with LBX encoded data.
39+
def encode(image_in: Image):
40+
"""Converts a RGB `Image` to a `io.BytesIO` with LBX encoded data.
4141
4242
Args:
43-
image (`PIL.Image`): The image to encode.
43+
image_in: The image to encode.
4444
4545
Returns:
4646
A `io.BytesIO` containing the LBX encoded image.
4747
"""
48-
im = image.convert('RGBA')
49-
pixel_words = np.array(im).reshape(-1, 4)
48+
image = image_in.convert('RGBA')
49+
pixel_words = np.array(image).reshape(-1, 4)
5050
pixel_words.flags.writeable = False
5151

52-
colormap = list(filter(lambda x: not np.all(x == _BACKGROUND_RGBA),
53-
np.unique(pixel_words, axis=0)))
52+
colormap = list(
53+
filter(lambda x: not np.all(x == _BACKGROUND_RGBA), np.unique(pixel_words, axis=0)))
5454

55-
56-
input_byte_len = len(np.array(im).flat)
55+
input_byte_len = len(np.array(image).flat)
5756
buff = BytesIO(bytes([0] * (len(colormap) * 4 + input_byte_len)))
5857

5958
offset = _HEADER_LENGTH # make room for header
@@ -67,64 +66,64 @@ def _color_to_key(color):
6766
struct.pack_into('<BBBB', buff.getbuffer(), offset, *color)
6867
color_dict[_color_to_key(color)] = i
6968
offset += 4
70-
color_dict[_color_to_key(_BACKGROUND_RGBA)] = i+1
69+
color_dict[_color_to_key(_BACKGROUND_RGBA)] = len(colormap)
7170

7271
count = 0
7372
pixel_ints = np.apply_along_axis(_color_to_key, 1, pixel_words)
74-
for i in range(len(pixel_ints)):
73+
for i, pixel_int in enumerate(pixel_ints):
7574
count += 1
7675
if i+1 == len(pixel_ints) \
77-
or not pixel_ints[i] == pixel_ints[i+1] \
76+
or not pixel_int == pixel_ints[i+1] \
7877
or count > 65534:
79-
struct.pack_into('<HH', buff.getbuffer(), offset,
80-
color_dict[pixel_ints[i]], count)
78+
struct.pack_into('<HH', buff.getbuffer(), offset, color_dict[pixel_int], count)
8179
offset += 4
8280
count = 0
8381

8482
# write header
85-
struct.pack_into('<iiiiii', buff.getbuffer(), 0,
86-
1, im.width, im.height, input_byte_len, len(colormap),
87-
offset - len(colormap)*4 - _HEADER_LENGTH)
83+
struct.pack_into(
84+
'<iiiiii', buff.getbuffer(), 0,
85+
1, image.width, image.height, input_byte_len,
86+
len(colormap), offset - len(colormap)*4 - _HEADER_LENGTH)
8887

8988
return buff
9089

9190

92-
def decode(lbx):
93-
"""Decodes a `io.BytesIO` with LBX encoded data into a `PIL.Image`.
91+
def decode(lbx: BytesIO):
92+
"""Decodes a `BytesIO` with LBX encoded data into a `PIL.Image`.
9493
9594
Args:
96-
lbxA (`io.BytesIO`): A byte buffer containing the LBX encoded image data.
95+
lbx: A byte buffer containing the LBX encoded image data.
9796
9897
Returns:
9998
A `PIL.Image` of the decoded data.
10099
"""
101-
version, width, height, byteLength, numColors, numBlocks = \
102-
map(lambda x: x[0], struct.iter_unpack('<i', lbx.read(_HEADER_LENGTH)))
100+
version, width, height, byte_length, num_colors, num_blocks = \
101+
map(lambda x: x[0], struct.iter_unpack('<i', lbx.read(_HEADER_LENGTH)))
102+
assert version == 1, 'this method only supports LBX v1 format'
103+
103104
colormap = np.array(
104-
list(_grouper(
105-
map(lambda x: x[0],
106-
struct.iter_unpack('<B', lbx.read(4 * numColors))),
107-
4)) +
108-
[_BACKGROUND_RGBA]
109-
)
105+
list(_grouper(
106+
map(lambda x: x[0],
107+
struct.iter_unpack('<B', lbx.read(4 * num_colors))),
108+
4)) +
109+
[_BACKGROUND_RGBA])
110110

111111
image_data = np.zeros((width * height, 4), dtype='uint8')
112112
offset = 0
113-
for _ in range(numBlocks):
113+
for _ in range(num_blocks):
114114
layer = struct.unpack('<H', lbx.read(2))[0]
115115
run_length = struct.unpack('<H', lbx.read(2))[0]
116116
image_data[offset:offset + run_length, :] = colormap[layer]
117117
offset += run_length
118-
assert offset == width * height, 'number of bytes read does not equal numBytes in header'
118+
assert 4*offset == byte_length, \
119+
'number of bytes read does not equal numBytes in header'
119120

120121
reshaped_image = np.reshape(image_data, (height, width, 4))
121-
im = Image.fromarray(reshaped_image, mode='RGBA')
122-
return im
122+
return Image.fromarray(reshaped_image, mode='RGBA')
123123

124124

125-
def _grouper(iterable, n, fillvalue=None):
125+
def _grouper(iterable, group_size, fillvalue=None):
126126
"Collect data into fixed-length chunks or blocks"
127127
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
128-
args = [iter(iterable)] * n
128+
args = [iter(iterable)] * group_size
129129
return itertools.zip_longest(*args, fillvalue=fillvalue)
130-

tests/test_lbx.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66
import pytest
77
import struct
88

9+
910
@pytest.fixture
1011
def im_png(datadir):
1112
with open(datadir.join('sample.png'), 'rb') as f:
1213
yield Image.open(BytesIO(f.read()))
1314

15+
1416
@pytest.fixture
1517
def lbx_sample(datadir):
1618
with open(datadir.join('sample.lbx'), 'rb') as f:
1719
yield f
1820

21+
1922
def test_lbx_decode(lbx_sample):
2023
im = lbx.decode(lbx_sample)
2124
assert im.size == (500, 375)
2225

26+
2327
def test_lbx_encode(im_png):
2428
lbx_encoded = lbx.encode(im_png)
2529
version, width, height = map(lambda x: x[0], struct.iter_unpack('<i', lbx_encoded.read(12)))
@@ -30,4 +34,3 @@ def test_lbx_encode(im_png):
3034

3135
def test_identity(im_png):
3236
assert np.all(np.array(lbx.decode(lbx.encode(im_png))) == np.array(im_png))
33-

0 commit comments

Comments
 (0)