Compressing and Saving an Image as Webp gives Corrupted image

Godot Version

Godot Engine v4.1.3.stable

Question

Hi im trying to compress an image and save it as webp to reduce the size of the image
when dragging a profile pic to the program. but the image that i get is corrupted though when i set the compressed image to a TextureRect it looks fine

Code

func image_corrupt_replicate():
	var compressed_image = Image.load_from_file("C:/User/Pictures/Image.png")
	compressed_image.compress(Image.COMPRESS_ASTC, Image.COMPRESS_SOURCE_GENERIC, Image.ASTC_FORMAT_8x8)
	compressed_image.save_webp("C:/User/Pictures/CompressedImage.webp", true)

When you say corrupted, are you saying an image application will not open it, or it opens it and it’s garbled?

i can open it but it looks like this:

UserPFP

How are you viewing it, an external image application, if so, which one? I think webp is a newer format, so maybe the application you are using doesn’t support it well.

what do you mean how am i viewing it? i tried saving it as a PNG its the same so it doesn’t have anything to do with webp. and i tried loading the image to the app to use it and it still corrupted

You are compressing with ASTC, then trying to save the resulting image as WebP. I’m pretty sure that’s unsupported, and a bad idea anyway. WebP applies its own compression to the image, which means that the image should be uncompressed when it is saved as WebP. Layering multiple compression algorithms results in worse overall compression at best and data corruption at worst.

Is there another way to reduce the size of the image?

You can turn down the quality when saving as WebP. Lower quality = smaller file.

1 Like