Difference between revisions of "ImageData:getPixel"

(Noting the ImageData pixel range)
m
Line 1: Line 1:
Gets the pixel at the specified position.
+
Gets the color of a pixel at a specific position in the image.
  
 
Valid x and y values start at 0 and go up to image width and height minus 1.
 
Valid x and y values start at 0 and go up to image width and height minus 1.
Line 15: Line 15:
 
{{param|number|b|The blue component (0-255).}}
 
{{param|number|b|The blue component (0-255).}}
 
{{param|number|a|The alpha component (0-255).}}
 
{{param|number|a|The alpha component (0-255).}}
 +
== Examples ==
 +
Iterates over the pixels of an image and stores them in a [[sequence]].
 +
<source lang="lua">
 +
local image = love.image.newImageData( 'path/to/Image.png' )
 +
local pixels = {}
 +
for x = 1, image:getWidth() do
 +
    for y = 1, image:getHeight() do
 +
        -- Pixel coordinates range from 0 to image width - 1 / height - 1.
 +
        local pixel = image:getPixel( x - 1, y - 1 )
 +
        pixels[#pixels + 1] = pixel
 +
    end
 +
end
 +
return pixels;
 +
</source>
 
== See Also ==
 
== See Also ==
 
* [[parent::ImageData]]
 
* [[parent::ImageData]]
 +
* [[ImageData:setPixel]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Gets the pixel at the specified position.}}
+
{{#set:Description=Gets the color of a pixel.}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|ImageData:getPixel}}
 
{{i18n|ImageData:getPixel}}

Revision as of 09:03, 1 June 2016

Gets the color of a pixel at a specific position in the image.

Valid x and y values start at 0 and go up to image width and height minus 1.

Function

Synopsis

r, g, b, a = ImageData:getPixel( x, y )

Arguments

number x
The position of the pixel on the x-axis.
number y
The position of the pixel on the y-axis.

Returns

number r
The red component (0-255).
number g
The green component (0-255).
number b
The blue component (0-255).
number a
The alpha component (0-255).

Examples

Iterates over the pixels of an image and stores them in a sequence.

local image = love.image.newImageData( 'path/to/Image.png' )
local pixels = {}
for x = 1, image:getWidth() do
    for y = 1, image:getHeight() do
        -- Pixel coordinates range from 0 to image width - 1 / height - 1.
        local pixel = image:getPixel( x - 1, y - 1 )
        pixels[#pixels + 1] = pixel
    end
end
return pixels;

See Also


Other Languages