Image.set_pixel() doesn't work

Godot Version

Godot 4.2

Question

I have a problem that Image.set_pixel() doesn’t draw anything on a Image.

Code:
Screenshot_20240107_124205

And the result it’s just red square without anything green.


the code:

func _ready():
	var image= Image.create(512,512, false,Image.FORMAT_RGBA8)
	image.fill(Color.RED)
	for x in range(256):
		for y in range(256):
			image.set_pixel(x,y,Color.GREEN)
	var texture=ImageTexture.create_from_image(image)
	self.texture=texture

Oh, right now i discovered that it’s not problem with the code but i just used Sprite2D to renderer this texture and when i switched to TextureRect everything seems to work well.

that one i was using Sprite2D btw, it’s working here, not sure what’s the issue

It’s also not problem with renderer, interesting.

Just have run into the same issue:


Code:

extends Node2D

class_name DrawingCanvas

var image: Image

func _draw():
	draw_texture(ImageTexture.create_from_image(image), Vector2.ZERO)

func _ready():
	image = Image.create(10, 5, false, Image.FORMAT_RGBA8)
	image.fill(Color.RED)
	image.set_pixel(2, 2, Color.GREEN)
	image.set_pixel(3, 2, Color.GREEN)
	image.set_pixel(3, 3, Color.GREEN)
	image.resize(300, 150)
	queue_redraw()

methods fill and set_pixel does not work, but resize does.

Changing script to store that texture as a variable fixed it.

extends Node2D

class_name DrawingCanvas

var image: Image
var texture: Texture
func _draw():
	draw_texture(texture, Vector2.ZERO)

func _ready():
	image = Image.create(10, 5, false, Image.FORMAT_RGBA8)
	image.fill(Color.RED)
	image.set_pixel(2, 2, Color.GREEN)
	image.set_pixel(3, 2, Color.GREEN)
	image.set_pixel(3, 3, Color.GREEN)
	image.resize(300, 150)
	texture = ImageTexture.create_from_image(image)
	queue_redraw()

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