# Getting raw pixel data from UARTexture

**URL:** <https://forums.unrealengine.com/t/getting-raw-pixel-data-from-uartexture/485567>\
**Category:** Rendering\
**Tags:** unreal-engine, Augmented-Reality, question\
**Created:** [June 30, 2021, 1:07pm UTC](https://forums.unrealengine.com/t/getting-raw-pixel-data-from-uartexture/485567 "2021-06-30T13:07:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timo\_Engelke](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Timo\_Engelke](https://forums.unrealengine.com/u/Timo_Engelke)\
**Post date:** [June 30, 2021, 1:07pm UTC](https://forums.unrealengine.com/t/getting-raw-pixel-data-from-uartexture/485567/1 "2021-06-30T13:07:08Z")

</div>

We retrieve an UARTexture from the blueprint function “GetARTexture()” of the ARBlueprintLibrary (having “Camera Image” selected).

From this UARTexture, we want to retrieve the raw pixel data, but until now we don’t have any success.

We found a couple solutions, that were based on UTexture2D - like for example: [UE4 - Reading the pixels from a UTexture2D - Isara Tech.](https://isaratech.com/ue4-reading-the-pixels-from-a-utexture2d/) , but UARTexture only derives from UTexture and not from UTexture2D.

The only one that we found which should work for UTexture and therefor for UARTexture as well, uses the editor-only property “Source” and therefor also doesn’t work:

> **[Getting UTexture raw pixel data](https://forums.unrealengine.com/questions/886686/view.html)**
>
> Hi, Can someone hint me how I can get raw pixel data from the UTexture I retreive from a material? I am stuck at the point where I need to get actual pixel data I have no idea where to get documentation for it, because in official docs there is not...

Best Regards,

Michael

---

<div class="post-metadata">

**Author:** ![Moo\_Chung](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Moo\_Chung](https://forums.unrealengine.com/u/Moo_Chung)\
**Post date:** [July 8, 2021, 8:35am UTC](https://forums.unrealengine.com/t/getting-raw-pixel-data-from-uartexture/485567/2 "2021-07-08T08:35:40Z")

</div>

> <https://github.com/zhmxiaowo/SpaceTarget/blob/main/Source/SpaceTarget/Private/ARCoreSlamGenerator.cpp>

you can see what we done in different platform.i have search for a month.  
it will return the unit8\* raw data.

---

<div class="post-metadata">

**Author:** ![su-020297](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@su-020297](https://forums.unrealengine.com/u/su-020297)\
**Post date:** [November 16, 2022, 5:57am UTC](https://forums.unrealengine.com/t/getting-raw-pixel-data-from-uartexture/485567/3 "2022-11-16T05:57:33Z")

</div>

It seems the code below works for me.

> **[UE4でUTextureをPNGのTArrayに変換する - Qiita](https://qiita.com/mechamogera/items/a0c369a3b853a3042cae)**
>
> オンメモリでテクスチャをPNG形式にしてアップロードしたかったのでUTextureをPNGのTArray形式に変換してみました。
> UE4.27.2、UE5.1.1で確認しています。
> 以下のコードを参照しています(ほぼそのままです)。
> ...

 ![GetTexturePixels](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/4/4/d44275afa224910589200833d2a6e340af453543.png)  
 ![UsingGetTexturePixels](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/2/e/72eaccd157e5b59197b6c97b1cd17a8586c4f151.png)

---

<div class="post-metadata">

**Author:** ![CKBksjflsf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ckbksjflsf/32/775878_2.png) [@CKBksjflsf](https://forums.unrealengine.com/u/CKBksjflsf)\
**Post date:** [November 14, 2023, 5:55pm UTC](https://forums.unrealengine.com/t/getting-raw-pixel-data-from-uartexture/485567/4 "2023-11-14T17:55:33Z")

</div>

It works, Thanks.  
But some minor changed should be applied in the code.  
In my case, Texture2D-\>GetSizeXY() return 1\*1, so I change to ARTexture-\>Size.XY and it works.  
Here’s my code:

```auto
void UMyARLib::GetARTextureData(UARTexture* ARTexture)
{
	FTexture2DRHIRef Texture2D = ARTexture->GetResource()->TextureRHI->GetTexture2D();
	if(!Texture2D)
	{
		UE_LOG(LogTemp, Warning, TEXT("Texture2D failed"));
		return;
	}

	TArray<FColor> OutPixels;
	struct FReadSurfaceContext
	{
		FTexture2DRHIRef Texture;
		TArray<FColor>* OutData;
		FIntRect Rect;
		FReadSurfaceDataFlags Flags;
	};

	FReadSurfaceContext ReadSurfaceContext =
	{
		Texture2D,
		&OutPixels,
		FIntRect(0, 0, ARTexture->Size.X, ARTexture->Size.Y),
		FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX)
	};

	FReadSurfaceContext Context = ReadSurfaceContext;
	ENQUEUE_RENDER_COMMAND(ReadSurfaceCommand)(
		[Context](FRHICommandListImmediate& RHICmdList)
		{
			RHICmdList.ReadSurfaceData(
				Context.Texture,
				Context.Rect,
				*Context.OutData,
				Context.Flags);
		});
	FlushRenderingCommands();

	//Save to file
	TArray64<uint8> ImgData;
	FImageUtils::PNGCompressImageArray(Context.Rect.Width(), Context.Rect.Height(), OutPixels, ImgData);
	FString FileName = FPaths::ProjectSavedDir() + "test.png";
	FFileHelper::SaveArrayToFile(ImgData, *FileName);
	
}

```
