How does EditableImage:WritePixels() work?

Just wondering how this function works, since there is no documentation on it.

I’m mainly going to use this to remove all the pixels to make a fresh canvas! There is no :Clear() method.

-- documentation:
EditableImage:WritePixels(position: Vector2, size: Vector2, pixels: {any})
3 Likes

It takes in a table and reads it from top left, top-bottom. The table is equal to the size of the canvas (x*y) and has 4 values for each pixel, meaning a 10x10 image has 400 indexes in the table.
EditableImage:WritePixels(Vector2.new(0,0),Vector2.new(2,2),{255,0,0,0,200,255,0,0.5,255,255,255,0,0,0,0,1}) paints the 4 pixels in the top left corner.
The table is read in fours, so {Red, Green, Blue, Transparency}.

4 Likes

It would be easier to just destroy it and create a new one.

2 Likes

For my use case, this would not be feasible. It would also be beneficial for information on the method to be posted on the devforum!

This seems very messy, are you sure that’s how it works? Have you tested it?

1 Like

Yes. They don’t separate how it’s read, just a very long array

2 Likes

If you can’t destroy it, DrawRectangle would be a better choice to avoid wasting memory and CPU time.

image:DrawRectangle(Vector2.zero, image.Size, Color3.new(1,1,1), 0)

2 Likes

That won’t work for me either. Please, only reply if you know exactly how WritePixels works.

2 Likes

its like @theseformasentence said, but the color values must be between 0 and 1, the same way you would use Color3.new(), and the last one is actually alpha, not transparency

EditableImage:WritePixels(
    Vector2.new(123, 234), -- position
    Vector2.one, -- size
    {
        1, -- r
        0, -- g
        0, -- b
        1, -- alpha, 0 = fully transparent
    }
)

Basically every 4 table entries is 1 pixel

yes there is

3 Likes

That’s literally what you asked for… If you want a roundabout way of achieving this, but just worse, here you go.

local Pixels = table.create(image.Size.X*image.Size.Y*4, 1);
image:WritePixels(Vector2.zero, image.Size, Pixels);
1 Like

im still confuse how pixel array works like its so confusing

1 Like

You specify the top left corner as your position, and the size of the rectangle you want to fill. Each 4 numbers in the array represent the r, g, b and alpha values of each pixel, left to right, row by row inside the mentioned rectangle.

1 Like

image
can you tell me what did i do wrong?

1 Like

Since it’s an array with 4 numbers, its the same as filling only one pixel.

local pixels = table.create(dyi.Size.X*dyi.Size.Y*4, 1); -- create a blank white rectangle
// Replace the values with (1,1,1,0)
for x=0, dyi.Size.X-1 do
    for y=0, dyi.Size.Y-1 do
        local i0 = y * dyi.Size.X + x + 1;  -- y * horizontal size + x + 1
        pixels[i0+0] = 1;
        pixels[i0+1] = 1;
        pixels[i0+2] = 1;
        pixels[i0+3] = 0;
    end
end
image:WritePixels(
    Vector2.new(0, 0),
    dyi.Size,
    pixels
)

Also, setting alpha to 0 will make it transparent, so you might want to replace it with 1.

1 Like

so i made this script

local UI = script.Parent
local DYI = Instance.new("EditableImage",UI.ImageLabel)

for x = 1,DYI.Size.X do
	for y = 1,DYI.Size.Y do
		DYI:WritePixels(Vector2.new(x, y),Vector2.one, {1,0,0,1})
		game:GetService('RunService').RenderStepped:Wait()
	end
end

and it gave me this result

not what i expected but it works now i need to explore this more and btw why not use color3.new instead of 1,0,1 right?

1 Like

Because color3 is expensive to create in comparison. It’s a method call that creates a userdata associated with an array of 3 floats. 3 numbers are just 3 numbers. It gets expensive fast when a 1000x1000 image would require a million color3s, and that wouldn’t even fill an average laptop’s resolution.

1 Like

so i used your script and it kinda worked but not what way i wanted it to

local UI = script.Parent
local DYI = Instance.new("EditableImage",UI.ImageLabel)
local pixels = table.create(DYI.Size.X*DYI.Size.Y*4, 1)

for x=0, DYI.Size.X-1 do
	for y=0, DYI.Size.Y do
		local i0 = y * DYI.Size.X + x + 1;
		pixels[i0+0] = 1;
		pixels[i0+1] = 1;
		pixels[i0+2] = 0;
		pixels[i0+3] = 1;
	end
end

DYI:WritePixels(Vector2.new(0, 0),DYI.Size,pixels)

is it suppose to be like this?

1 Like

idk what i have done but i made it cut the frame?? :skull:

1 Like

I wonder if the pixels start at 1, 1 instead of 0, 0

1 Like

i think it starts at the top left

1 Like