Skip to content

Commit 0747939

Browse files
committed
Working encode test
1 parent e1cbec6 commit 0747939

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

labelbox/lbx.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,47 @@
2424
bytes 0-1 -> R | G value of pixel (superpixel layer #)
2525
bytes 2-3 -> # consecutive occurences
2626
"""
27+
from io import BytesIO
2728
import itertools
2829
import struct
2930

3031
import numpy as np
3132
from PIL import Image
3233

34+
_BACKGROUND_RGBA = [255, 255, 255, 255]
35+
_HEADER_LENGTH = 6 * 4
36+
3337

3438
def encode(image):
35-
"""Converts a `PIL.Image` to a `io.BytesIO` with LBX encoded data.
39+
"""Converts a RGB `PIL.Image` to a `io.BytesIO` with LBX encoded data.
3640
3741
Args:
3842
image (`PIL.Image`): The image to encode.
3943
4044
Returns:
4145
A `io.BytesIO` containing the LBX encoded image.
4246
"""
43-
return image
47+
im = image.convert('RGBA')
48+
pixel_words = np.array(im).reshape(-1, 4)
49+
50+
colormap = list(filter(lambda x: not np.all(x == _BACKGROUND_RGBA),
51+
np.unique(pixel_words, axis=0)))
52+
53+
54+
input_byte_len = len(np.array(im).flat)
55+
buff = BytesIO(bytes([0] * (len(colormap) * 4 + input_byte_len)))
56+
57+
offset = _HEADER_LENGTH # make room for header
58+
for color in colormap:
59+
struct.pack_into('<BBBB', buff.getbuffer(), offset, *color)
60+
offset += 4
61+
62+
# write header
63+
struct.pack_into('<iiiiii', buff.getbuffer(), 0,
64+
1, im.width, im.height, input_byte_len, len(colormap),
65+
offset - len(colormap) - _HEADER_LENGTH)
66+
67+
return buff
4468

4569

4670
def decode(lbx):
@@ -53,13 +77,13 @@ def decode(lbx):
5377
A `PIL.Image` of the decoded data.
5478
"""
5579
version, width, height, byteLength, numColors, numBlocks = \
56-
map(lambda x: x[0], struct.iter_unpack('<i', lbx.read(24)))
80+
map(lambda x: x[0], struct.iter_unpack('<i', lbx.read(_HEADER_LENGTH)))
5781
colormap = np.array(
5882
list(_grouper(
5983
map(lambda x: x[0],
6084
struct.iter_unpack('<B', lbx.read(4 * numColors))),
6185
4)) +
62-
[[0, 0, 0, 255]]
86+
[_BACKGROUND_RGBA]
6387
)
6488

6589
image_data = np.zeros((width * height, 4), dtype='uint8')

tests/test_lbx.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import labelbox.lbx as lbx
44
from PIL import Image
55
import pytest
6+
import struct
67

78
@pytest.fixture
89
def im_png(datadir):
@@ -20,12 +21,12 @@ def test_lbx_decode(lbx_sample):
2021

2122
def test_lbx_encode(im_png):
2223
lbx_encoded = lbx.encode(im_png)
23-
version, width, height = map(lambda x: x[0], struct.iter_unpack('<i', lbx.read(12)))
24+
version, width, height = map(lambda x: x[0], struct.iter_unpack('<i', lbx_encoded.read(12)))
2425
assert version == 1
2526
assert width == 800
2627
assert height == 600
2728

2829

29-
# def test_identity(im_png):
30-
# assert np.all(np.array(lbx.decode(lbx.encode(im_png))) == np.array(im_png))
30+
def test_identity(im_png):
31+
assert np.all(np.array(lbx.decode(lbx.encode(im_png))) == np.array(im_png))
3132

0 commit comments

Comments
 (0)