# OpenCV YUV420 to RGB

**URL:** <https://forum.opencv.org/t/opencv-yuv420-to-rgb/18373>\
**Category:** Python\
**Tags:** raspberrypi\
**Created:** [July 29, 2024, 6:25pm UTC](https://forum.opencv.org/t/opencv-yuv420-to-rgb/18373 "2024-07-29T18:25:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zalosath](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zalosath/32/10850_2.png) [@Zalosath](https://forum.opencv.org/u/Zalosath)\
**Post date:** [July 29, 2024, 6:25pm UTC](https://forum.opencv.org/t/opencv-yuv420-to-rgb/18373/1 "2024-07-29T18:25:25Z")

</div>

Hi, I’m trying to figure out how to convert a YUV420 image to an RGB image, but it’s not working:

```auto
buffer = self.model.picam2.capture_buffer("lores")

self.logger.debug(f"Captured frame: {buffer}")
self.logger.debug(f"Captured frame shape: {buffer.shape}")

rgb = cv2.cvtColor(buffer, cv2.COLOR_YUV420p2RGB)

ret, jpeg = cv2.imencode('.jpg', rgb)
if not ret:
    self.logger.error("Failed to encode frame as JPEG")
    continue

yield (b'--frame\r\n'
    b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')

```

The error that this gives is:

> Error in video feed: OpenCV(4.6.0) ./modules/imgproc/src/color.simd\_helpers.hpp:108: error: (-215:Assertion failed) sz.width % 2 == 0 && sz.height % 3 == 0 in function ‘CvtHelper’

So I printed the shape and frame, and this is the result:

```auto
Captured frame: [118 120 122 ... 137 137 140]
Captured frame shape: (460800,)

```

What am I doing wrong here? [This source](https://github.com/raspberrypi/picamera2/blob/main/examples/yuv_to_rgb.py#L16) suggests that this should work

---

<div class="post-metadata">

**Author:** ![Zalosath](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zalosath/32/10850_2.png) [@Zalosath](https://forum.opencv.org/u/Zalosath)\
**Post date:** [July 29, 2024, 7:23pm UTC](https://forum.opencv.org/t/opencv-yuv420-to-rgb/18373/2 "2024-07-29T19:23:10Z")

</div>

I solved it, I was using capture\_buffer, not capture\_array. Oops!
