How do you use data.set_pixel ?

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

Hello,
I’m sampling an image with data.get_pixel.
So far it is working (I get the colors) , but I’m unable to data.set_pixel the pixel (rewrite them at another place)
Here’s my code

extends Node2D

var GroundImg = preload("res://images/UV_200.jpg")

func _draw(): 
var data = GroundImg.get_data()

data.lock()

var pixel = data.get_pixel(fSampleX,fSampleY)

data.set_pixel(x,y,Color (pixel))

data.unlock()
update()

Any advice ?

Thank’s !

If I understand that code corrrectly then you write that color exactly back where you got it. So it shouldn’t change.

But what are you expecting? Do you want to save those changes back to the *.jpg or show it on screen?

Ok, I see that you do that in _draw() but doing set_pixel on some image would not draw this automatically to the screen.

If you want to change pixels inside some Node2D descendant then a fragment shader might perform much better. (Depending on what you’re actually trying to do.)

wombatstampede | 2019-11-11 14:00