# How can I convert YUV\_420\_888 to JPG in C#

**URL:** <https://forum.magicleap.cloud/t/how-can-i-convert-yuv-420-888-to-jpg-in-c/3740>\
**Category:** Unity Development\
**Tags:** development, unity, camera\
**Created:** [July 4, 2024, 1:38pm UTC](https://forum.magicleap.cloud/t/how-can-i-convert-yuv-420-888-to-jpg-in-c/3740 "2024-07-04T13:38:37Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![uros.mitrovic](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@uros.mitrovic](https://forum.magicleap.cloud/u/uros.mitrovic)\
**Post date:** [July 10, 2024, 12:00pm UTC](https://forum.magicleap.cloud/t/how-can-i-convert-yuv-420-888-to-jpg-in-c/3740/5 "2024-07-10T12:00:33Z")

</div>

As I said I tried with RGB but it doesn't allow me to capture with CV while streaming, but when I use YUV it does. I found this answer and that's why I'm trying with this approach:

> [@MLCamera.CaptureType.Image and MLCamera.Identifier.CV compatibility](https://forum.magicleap.cloud/t/mlcamera-capturetype-image-and-mlcamera-identifier-cv-compatibility/3005/3):
>
> Hi @rscanlo2, The Magic Leap 2's CV Camera stream does not support JPG image capture at this time. You will need to either use the Main Camera or capture the image in YUV format and convert the image to RGB in a separate step. Here is a previous post that includes a sample of async JPG image capture in Unity using the Main Camera : [Camera capture in Unity - #14 by kbabilinski](https://forum.magicleap.cloud/t/camera-capture-in-unity/2718/14)

In meantime I managed to get black and white jpeg image using following code:

```auto
public static byte[] ConvertYUVToJPEG(byte[] yPlane, byte[] uPlane, byte[] vPlane, int width, int height)
{
    Color32[] rgbData = new Color32[width * height];
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            int yIndex = y * width + x;
            byte Y = yPlane[yIndex];
            // Convert Y to RGB (black and white)
            byte R = Y;
            byte G = Y;
            byte B = Y;
            rgbData[yIndex] = new Color32(R, G, B, 255);
        }
    }

    Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
    texture.SetPixels32(rgbData);
    texture.Apply();

    //FlipTextureVertically(texture);

    byte[] jpegData = texture.EncodeToJPG();

    Debug.Log("JPEG image saved at output.jpg");

    return jpegData;
}

```

Can you assist me with adapting this code for getting the colored image?

---

_[View the full topic](https://forum.magicleap.cloud/t/how-can-i-convert-yuv-420-888-to-jpg-in-c/3740)._
