Skip to content

Commit 6f55ad0

Browse files
Numpy arrays must be in BGR order not RGB - Issue #34 (#55)
* Addresses issue #34, assume numpy image arrays are in BGR to match opencv * updated docs * Update docs/docs/installation/libraries-numpy-pil.md Co-authored-by: robotrapta <[email protected]> --------- Co-authored-by: robotrapta <[email protected]>
1 parent 5079255 commit 6f55ad0

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

docs/docs/installation/libraries-numpy-pil.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ wrong if you try to use these features.
1111

1212
## Numpy
1313

14-
The Groundlight SDK can accept images as `numpy` arrays. They should be in the standard HWN format in RGB color order.
14+
The Groundlight SDK can accept images as `numpy` arrays. They should be in the standard HWN format in BGR color order, matching OpenCV standards.
1515
Pixel values should be from 0-255 (not 0.0-1.0 as floats). SO `uint8` data type is preferable since it saves memory.
1616

1717
Here's sample code to create an 800x600 random image in numpy:
@@ -22,17 +22,17 @@ import numpy as np
2222
img = np_img = np.random.uniform(0, 255, (600, 800, 3))
2323
```
2424

25+
If you have an RGB array, you must reverse the channel order before sending it to Groundlight, like:
26+
27+
```python notest
28+
bgr_img = rgb_img[:, :, ::-1]
29+
```
30+
31+
2532
## PIL
2633

2734
The Groundlight SDK can accept PIL images directly in `submit_image_query`.
2835

2936
## OpenCV
3037

31-
OpenCV creates images that are stored as numpy arrays. So can send them to `submit_image_query` directly.
32-
<b>BUT!</b> OpenCV uses BGR color order, not RGB. You can reverse them as follows:
33-
34-
```python notest
35-
rgb_img = bgr_img[:, :, ::-1]
36-
```
37-
38-
See [Issue #34](https://github.com/groundlight/python-sdk/issues/34) for a discussion about OpenCV support.
38+
OpenCV creates images that are stored as numpy arrays. So can send them to `submit_image_query` directly.

docs/docs/installation/linux-windows-mac.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ To install the Groundlight SDK using pip, run the following command in your term
2121
pip install groundlight
2222
```
2323

24-
If you're using `python3`, you might need to use `pip3` instead:
24+
If you're also using `python2` on your system, you might need to use `pip3` instead:
2525

2626
```bash
2727
pip3 install groundlight

src/groundlight/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def submit_image_query(
149149
:param image: The image, in several possible formats:
150150
- filename (string) of a jpeg file
151151
- byte array or BytesIO or BufferedReader with jpeg bytes
152-
- numpy array with values 0-255 and dimensions (H,W,3) in RGB order
152+
- numpy array with values 0-255 and dimensions (H,W,3) in BGR order
153153
(Note OpenCV uses BGR not RGB. `img[:, :, ::-1]` will reverse the channels)
154154
- PIL Image
155155
Any binary format must be JPEG-encoded already. Any pixel format will get

src/groundlight/images.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def parse_supported_image_types(
4949
# Already in the right format
5050
return image
5151
if isinstance(image, np.ndarray):
52-
return BytesIO(jpeg_from_numpy(image, jpeg_quality=jpeg_quality))
52+
# Assume it is in BGR format from opencv
53+
return BytesIO(jpeg_from_numpy(image[:, :, ::-1], jpeg_quality=jpeg_quality))
5354
raise TypeError(
5455
(
5556
"Unsupported type for image. Must be PIL, numpy (H,W,3) RGB, or a JPEG as a filename (str), bytes,"

0 commit comments

Comments
 (0)