Loaded resource as image file, this will not work on export: 'res://Image.png'.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Xiuhman

Hi, I’m trying to change the color of specific pixels on an image and it works, however, I get this warning every time I do so and I just want to know if there is any way of fixing it.

I get the following warning.

Loaded resource as image file, this will not work on export: ‘res://Assets/Sprites/Player/Player_Idle0.png’. Instead, import the image file as an Image resource and load it normally as a resource.

That warning doesn’t make the game crash or the function fail but I do change the color on the sprite quite often so I get this error as often.

Here’s the code I’m using to change the color of specific pixels.

func _on_ColorPickerButton_color_changed(color):
	var secondary_color = color
	secondary_color.v = color.v + 0.12
	var image_path = "res://Assets/Sprites/Player/Player_Idle0.png"
	var img = Image.new()
	var itex = ImageTexture.new()
	img.load(image_path)
	img.lock()
	for w in range(img.get_width()):
		for e in range(img.get_height()):
			if img.get_pixel(e, w).g8 == 115 and img.get_pixel(e, w).b8 == 133 and img.get_pixel(e, w).r8 == 0:
				img.set_pixel(e, w, secondary_color)
			elif img.get_pixel(e, w).g8 == 89 and img.get_pixel(e, w).b8 == 103 and img.get_pixel(e, w).r8 == 0:
				img.set_pixel(e, w, color)
	img.unlock()
	itex.create_from_image(img)
	$CenterContainer/VBoxContainer6/ColorSprite.texture = itex
	$CenterContainer/VBoxContainer6/ColorSprite.texture.set_flags(1)

It works fine except for that warning, and I did read that simply doing something like:

var img = load("res://Assets/Sprites/Player/Player_Idle0.png")

Would fix the problem, however, that returns a StreamTexture which cannot be modified like an Image.

Thx in advance.

:bust_in_silhouette: Reply From: Xrayez

Would fix the problem, however, that returns a StreamTexture which cannot be modified like an Image.

Every Texture derived class has a get_data method which returns an image associated with the texture, you can use that to modify the image and recreate the texture as ImageTexture.

I believe it’s also possible to import it as a native Image resource via Import tab, just select the image in the filesystem, and change “Import As” to “Image”, so that loading it with load shall return an image resource rather than a texture.

The get_data method did fix the problem but it leads to weird behavior when changing the colors however changing the “Import As” to “Image” did fix the problem completely. Thank you!

Xiuhman | 2019-08-22 15:42