Basic operations of images
Coordinates
Get/set pixels
We can get the value of a pixel through the image.get_pixel(x, y) method.
- image.get_pixel(x, y)
- For grayscale images: Returns the grayscale value of the (x, y) coordinate.
- For color images: Returns a tuple of (r, g, b) of the (x, y) coordinate.
Similarly, we can set the value of a pixel through the image.set_pixel(x, y, pixel) method.
- image.set_pixel(x, y, pixel)
- For grayscale images: Sets the grayscale value of the (x, y) coordinate.
- For color images: Sets the (r, g, b) value of the (x, y) coordinate.
Example:
img = sensor.snapshot()
img.get_pixel(10,10)
img.set_pixcel(10,10,(255,0,0))#设置坐标(10,10)的像素点为红色(255,0,0)
Get the width and height of an image
image.width()\ Returns the width of the image (in pixels)
image.height()\ Returns the height of the image (in pixels)
image.format()\ Grayscale images will return sensor.GRAYSCALE, and color images will return sensor.RGB565.
image.size()\ Returns the size of the image (byte)
Image operations
- image.invert()
Inverts, for binary images, 0 (black) becomes 1 (white), and 1 (white) becomes 0 (black).
Note:\ image can be another image object, or an image object read from a (bmp/pgm/ppm) file.\ Both images must be of the same size and type (grayscale/color).
image.nand(image)\ Performs a NAND operation with another image.
image.nor(image)\ Performs a NOR operation with another image.
image.xor(image)\ Performs an XOR operation with another image.
image.xnor(image)\ Performs an XNOR operation with another image.
image.difference(image)\ Subtracts another image from this one. For example, for each pixel in each channel, take the absolute value of the difference. This function is often used for motion detection.