How To Get Average Colour Of An Image?

I tried it as well, but I reversed the for loops. This might make a difference here, since the cpu can cache the rows easier than the columns.
I also tried both c# and gdscript, both are roughly equal in speed and dont take too long. I even tried the following big image from wallhaven, this took less than 2 seconds for me, most of the time was starting the scene

What are your components for your computer/ laptop?

here is my code:
The scene is a sprite2d with this script, with a colorRect child to test the color

@export var colorRect: ColorRect

func _ready() -> void:
	var color := Vector3.ZERO
	var texture_size := texture.get_size()
	var image := texture.get_image()
	
	for y in range(0, texture_size.y):
		for x in range(0, texture_size.x):
			var pixel := image.get_pixel(x, y)
			color += Vector3(pixel.r, pixel.g, pixel.b)
			
	color /= texture_size.x * texture_size.y

	colorRect.color = Color(color.x, color.y, color.z)


1 Like