I used it, but here is an error

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

I used it, but that gave an error: error(12,1): The identifier “pixel” isn’t declared in the current scope.

And my project:

func _ready():
	yield(VisualServer, "frame_post_draw")
	var img = $ViewportContainer/Viewport.get_texture().get_data()
	img.flip_y()
	img.crop(100, 100)
	img.save_png("img1.png")
	if pow(pixel.x - center.x, 2) / pow(radius.x, 2) + pow(pixel.y - center.y, 2) / pow(radius.y, 2) > 1:
		image.set_pixel(pixel, Color(0, 0, 0, 0))
:bust_in_silhouette: Reply From: USBashka

set_pixel() method gets 3 arguments: x, y and color. I see that you’re trying to crop image to circle, but for that you need put set_pixel() into cycle and do it before saving.

I hope this will work:

func _ready():
    yield(VisualServer, "frame_post_draw")
    var img = $ViewportContainer/Viewport.get_texture().get_data()
    img.flip_y()
    img.crop(100, 100)
    for x in 100:
        for y in 100:
            if (pow(x-50, 2) + pow(y-50, 2)) / pow(50, 2) > 1:
                image.set_pixel(x, y, Color(0, 0, 0, 0))
    img.save_png("img1.png")

Did not work

mohsenisaei | 2022-08-02 14:23

Sorry. What the result?

USBashka | 2022-08-02 14:30

Just crop 100x100 , rectangle shape.
No error is displayed and no information was given in this regard

mohsenisaei | 2022-08-03 08:39

Okay. I’ve fixed it and checked (it works përfect):

yield(VisualServer, "frame_post_draw")
var img = $ViewportContainer/Viewport.get_texture().get_data()
img.flip_y()
img.convert(Image.FORMAT_RGBA8) #The default format doesn't supported alpha
img.crop(100, 100)
img.lock()
for x in 100:
	for y in 100:
		if (pow(x-49.5, 2) + pow(y-49.5, 2)) / pow(50, 2) > 1:
			img.set_pixel(x, y, Color(0, 0, 0, 0))

USBashka | 2022-08-03 10:45

Thanks, it worked
Now, in this program, if I want to cut the middle of the image with the crop command, how is it done?
Note: Some images may have different dimensions from others, and we do not know what size of images are going to be imported.

mohsenisaei | 2022-08-09 10:29

also what you said cuts in a circle, is there a way to make it an oval?

mohsenisaei | 2022-08-10 07:45