Skip to content

Commit

Permalink
decode JPEG image data to numpy array (#29)
Browse files Browse the repository at this point in the history
* decode JPEG image data to numpy array

* moved image_decode.py into module folder. Added test file template. TODO: Finish writing tests when image encode is finished

* added dummy variable

* linter changes
  • Loading branch information
maxlou05 authored Apr 6, 2024
1 parent 2f309e4 commit 083b67c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lte/modules/image_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Decodes images from JPEG bytes to numpy array.
"""

import io

from PIL import Image
import numpy as np


def decode(data: "io.BytesIO | bytes") -> np.ndarray:
"""
Decodes a JPEG encoded image and returns it as a numpy array.
Args:
data: bytes object containing the JPEG encoded image
TODO: When encoding JPEG, make sure the input image only has 3 channels,
if original picture was a PNG (filter out A channel)
Returns:
NDArray with in RGB format. Shape is (Height, Width, 3)
"""
image = Image.open(data, formats=["JPEG"])

return np.asarray(image)
27 changes: 27 additions & 0 deletions lte/test_image_encode_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Test image encode and decode.
TODO: finish image encode and then write this test
"""

from lte.modules import image_decode


def main() -> int:
# TODO: Get test images and Encode
raw_data = 0

# Decode
img_array = image_decode.decode(raw_data)

# Do checks
# eg. check output shape
return 0


if __name__ == "__main__":
result_main = main()

if result_main < 0:
print(f"ERROR: Status code: {result_main}")

print("Done!")
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pytest
opencv-python
numpy

# Module lte
Pillow

# Module mavlink
dronekit

Expand Down

0 comments on commit 083b67c

Please sign in to comment.