Cannot get_pixel() on Compressed Image Formats

Godot Version

4.2.1

Question

I am writing a program in Godot that overlays textures on each other. I iterate over each pixel of the image (via Texture.get_image()) and manipulate their color values. It works perfectly when using textures such as GradientTextures and NoiseTextures, which are uncompressed.

When I import a CompressedTexture2D such as a PNG file and attempt to iterate over each of its pixels, I receive this error: get_image(): Can’t get_pixel() on compressed image, sorry. This makes sense because of how PNGs and many other image formats are stored.

I cannot figure out how to convert a compressed image (from a CompressedTexture2D) to an uncompressed image so that I can iterate over each pixel. I have tried using the blit_rect() function, but that does not work either.

How should I go about iterating over the pixels of an Image created from a CompressedTexture2D?

You’ll need to call Image.decompress() first before being able to access its data.

var image = texture.get_image()
if image.is_compressed():
	image.decompress()
print(image.get_pixel(0, 0))

If it’s an external image you could try loading the Image directly with Image.load_from_file()

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.