+1 vote

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.

in Engine by (65 points)

1 Answer

+3 votes
Best answer

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.

by (1,378 points)
selected by

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.