- Home /
Question by
erfanroghani225 · Dec 12, 2020 at 10:13 AM ·
screenshotgrayscale16 bits
How to take 16 bit/channel grayscale screenshots?
Hello all,
I need to take 16 (or 32) bit/channel screenshots from my grayscale view and load them to Python. I use the following code but it seems to be giving 8 bit/channel RGB images. Please let me know what I am doing wrong. Thank you,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenshotHandler: MonoBehaviour
{
private static ScreenshotHandler instance;
private Camera myCamera;
private bool takeScreenshotOnNextFrame;
private void Awake()
{
instance = this;
myCamera = gameObject.GetComponent<Camera>();
}
private void OnPostRender()
{
if (takeScreenshotOnNextFrame)
{
takeScreenshotOnNextFrame = false;
RenderTexture renderTexture = myCamera.targetTexture;
Texture2D renderResult = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.R16, false);
Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
renderResult.ReadPixels(rect, 0, 0);
byte[] byteArray = renderResult.EncodeToPNG();
System.IO.File.WriteAllBytes(Application.dataPath + "/CameraScreenshot.png", byteArray);
Debug.Log("Saved CameraScreenshot.png");
RenderTexture.ReleaseTemporary(renderTexture);
myCamera.targetTexture = null;
}
}
private void TakeScreenshot(int width, int height)
{
myCamera.targetTexture = RenderTexture.GetTemporary(width, height, 16);
takeScreenshotOnNextFrame = true;
}
public static void TakeScreenshot_Static(int width, int height)
{
instance.TakeScreenshot(width, height);
}
}
Comment
Your answer