# Convert loaded images to rgb565 results in color shift

**URL:** <https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950>\
**Category:** OpenMV Boards\
**Created:** [March 23, 2022, 8:06am UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950 "2022-03-23T08:06:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 23, 2022, 8:06am UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/1 "2022-03-23T08:06:58Z")

</div>

When I load images taken with the board and convert them to rgb565, a color shift occurs, also on firmware 4.2.3. This is visible on the new images below.

Original image taken by H7+, then cropped in gthumb and saved:

 ![22 - 321 ms - 5 dB - FNA_cropped gthumb](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/d/db5b1da14ab980cfaa70386229c29484bebf37de.jpeg)

Original image cropped and saved with H7+:

 ![22 - 321 ms - 5 dB - FNA.jpg_0_719_519_1062](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/0/02319c87f776aeb5f7da1247cb474c5b387c2550.jpeg)

As you see, the red cable becomes yellow.  
Original:

 ![22 - 321 ms - 5 dB - FNA](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/c/cf5c951b8c582957b5af6988d78f7e843af70a03.jpeg)  
This script can be used:

```auto
import sensor, image, time, pyb
img=image.Image("66.jpg",copy_to_fb=True)
print("original loaded image:",img)
img.save("66_straight.jpg")
img.to_rgb565()
print("rgb565-converted image:",img)
img.save("66_torgb.jpg")

```

When I load a jpeg and save it immediately after loading, no color shift is visible. If I do a to\_rgb565 conversion, the impact on colors can be dramatic, for example here (on another image):  
original:  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/9/9ef5278eab41bc1c72ec697d265d7d7f875f7574.png)

converted:  
 ![image](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/6/6f179b075faa24cd1d9dda578e1b1e09a1a10430.png)

---

<div class="post-metadata">

**Author:** ![iabdalkader](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/iabdalkader/32/1169_2.png) [@iabdalkader](https://forums.openmv.io/u/iabdalkader)\
**Post date:** [March 23, 2022, 10:50am UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/2 "2022-03-23T10:50:38Z")

</div>

Are you cropping the image then saving it as jpeg then reloading ? Can you post like steps to reproduce this issue ? It might be related to cropping on odd bayer boundaries.

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 23, 2022, 2:28pm UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/3 "2022-03-23T14:28:45Z")

</div>

I tried it with and without cropping (referring to the previous forum thread where this post was originally found), to then realise that cropping did not matter, to\_rgb565 conversion did. If you download any of the original images linked above and run the minimal working example, you will see the color shift. Here is the original image whose histogram is shown right above :

 ![10](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/9/989369380368de65b2043e6a8a44735e07d6c979.jpeg)  
By the way, it seems that img.format() returns the size and not the sensor format.

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [March 23, 2022, 5:14pm UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/4 "2022-03-23T17:14:34Z")

</div>

Can you post these issues on github so they can be tracked. This one and the jpeg decompress one? They both seem to stem from the same issue. I am free this weekend and can take a crake at fixing the hardware jpeg code.

---

<div class="post-metadata">

**Author:** ![iabdalkader](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/iabdalkader/32/1169_2.png) [@iabdalkader](https://forums.openmv.io/u/iabdalkader)\
**Post date:** [March 23, 2022, 5:43pm UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/5 "2022-03-23T17:43:46Z")

</div>

When you post an issue on github, please post a minimal script to reproduce the issue, steps and attach the images in a zip file.

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [March 24, 2022, 1:17am UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/6 "2022-03-24T01:17:47Z")

</div>

this was filed on GItHub:

> <https://github.com/openmv/openmv/issues/1597>
>
> When loading a jpeg saved on the SD card root, converting it to RGB565 and savin…g it as JPEG again, the resulting image will have a visible shift in color (less reddish)
> 
> Steps to reproduce the behavior:
> 1. Take full-resolution image(s) with the H7+ board (sensor.RGB565, sensor.snapshot(), img.save("xxx.jpeg",quality=92))
> 2. Copy jpeg(s) to SD card root
> 3. Run this:
> 
> \`\`\`python
> import image, os
> if not "output" in os.listdir(): os.mkdir("output")
> \#scan jpegs on card
> files=os.listdir()
> jpegs=\[files for files in files if "jpg" in files\]
> 
> for jpeg in jpegs:
> img=image.Image(jpeg,copy\_to\_fb=True)
> print("original loaded image:",img)
> img.save("straight\_"+jpeg)
>     
> img.to\_rgb565()
> print("rgb565-converted image:",img)
> img.save("torgb\_"+jpeg)
> 
> \`\`\`
> 4. Observe the non-converted ("straight") and rgb565-converted ("torgb565") images
> 
> I expected the script and board to output pictures with unaltered colors, whether they were converted to rgb565 or not. Instead, I get visibly different images:
> 
> Original image, loaded and then saved again as JPEG (it looks identical to the image zipped and attached below at the end of the post):
> !\[straight\_0 - dummy\](https://user-images.githubusercontent.com/7696198/159822565-a14a819b-5f44-4037-964a-157d826ac24b.jpg)
> 
> Original image, converted to RGB565, and then saved as JPEG:
> !\[torgb\_0 - dummy\](https://user-images.githubusercontent.com/7696198/159822607-a1118136-181c-4b0f-9242-1222f1327a54.jpg)
> 
> Original image:
> \[0 - dummy.zip\](https://github.com/openmv/openmv/files/8337639/0.-.dummy.zip)
> 
> - IDE version: 2.9.0
> - Firmware version: 4.3.0
> - Board: OPENMV-H7+
> - OS: Linux

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [April 11, 2022, 3:53am UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/7 "2022-04-11T03:53:10Z")

</div>

Thanks, this bug was fixed in the latest 4.3.1 firmware release - the color shift is much more subtle (there is still one but I suppose that is because the conversion cannot be lossless?).

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 11, 2022, 4:06pm UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/8 "2022-04-11T16:06:08Z")

</div>

Can you try this: [imlib: Enable hardware JPEG decoder. by kwagyeman · Pull Request #1604 · openmv/openmv · GitHub](https://github.com/openmv/openmv/pull/1604). The code here should have the best performance. I notice no color shifts. If you still see it then there’s nothing that can be done. It’s at the best.

---

<div class="post-metadata">

**Author:** ![darrask](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@darrask](https://forums.openmv.io/u/darrask)\
**Post date:** [April 19, 2022, 7:42am UTC](https://forums.openmv.io/t/convert-loaded-images-to-rgb565-results-in-color-shift/6950/9 "2022-04-19T07:42:57Z")

</div>

I tested it on more images. The issue appears solved and the slight changes I observed might be due to re-compression. Thanks a lot
