基本图像变换 和 常用操作

``````img.rotation_corr()
``````

``````img.resize()
``````

1. 图像缓冲区介绍

MaixPy 为图像设计了两个缓冲区，

• 一个是`RGB565`缓冲区，顾名思义， 是以`RGB565`的格式存放这图片的信息的一块内存。注意在内存中的排序是`[像素1 RGB, 像素2 RGB...]`
• 另一个是`RGB888`缓冲区，顾名思义， 是以`RGB88`的格式存放这图片的信息的一块内存。注意在内存中的排序是`[所有像素 R, 所有像素 G， 所有像素 B]`, 我们也称之为`AI`内存

``````                   +---------------+
|               |
+--------+ camera(sensor)+-------+
|        |               |       |
|        +---------------+       |
|                                |
+---------v------+                +--------v---------+
|                | img.pix_to_ai()|                  |
|      RGB565    +--------------->+      RGB888      |
|                |                |                  |
+--------+-------+                +------+-----------+
^                               |
|                               |
|                               v
+--------+----------+             +------+-----------+
|                   |             |                  |
|     image ops     |             |   KPU            |
|                   |             |                  |
+-------------------+             +------------------+
``````

2. resize 修改分辨率

``````import image
img = image.Image(size=(100, 100))
img2 = img.resize(50, 50)
print(img)
print(img2)
``````

3. 获取和修改像素值

``````import image
img = image.Image(size=(10, 10))
print("pixel 0:", img[0], img.get_pixel(0, 0))
img[0] = (255, 0, 0)
img = img.set_pixel(1, 0, (255, 255, 10))
print("after pixel 0 change:", img[0], img[1])
``````

4. 复制图像

``````import image
img = image.Image(size=(10, 10))
img2 = img.copy()
img2[0] = (255, 0, 0)
print(img[0], img2[0])
``````

5. 剪裁图像

``````import image
img = image.Image(size=(10, 10))
img2 = img.copy(roi=(0, 0, 5, 5))
img2[0] = (255, 0, 0)
print(img)
print(img2)
print(img[0], img2[0])
``````

6. 转换成 bytes 对象

``````import image
img = image.Image(size=(10, 10))
img[0] = (255, 0, 0)
img_bytes = img.to_bytes()
print("bytes length: %d bytes[0]: %x%x" %(len(img_bytes), img_bytes[0], img_bytes[1]))
``````

``````import image
img = image.Image(size=(10, 10))
img = img.compressed(quality=20)
jpeg_bytes = img.to_bytes()
print("bytes length: %d bytes[0]: %x%x" %(len(jpeg_bytes), jpeg_bytes[0], jpeg_bytes[1]))
``````

7. 转换为灰度图像

``````img = img.to_grayscale(copy=False)
``````

8. 转换为 RGB565 彩图

``````img = img.to_rgb565(copy=True)
``````

9. 转换为彩图

``````img = img.to_rainbow(copy=True)
``````

10. 保存到文件系统

``````img.save("/sd/test.jpg", quality=95)
img.save("/sd/test.bmp")
``````

11. 旋转

``````img.rotation_corr([x_rotation=0.0[, y_rotation=0.0[, z_rotation=0.0[, x_translation=0.0[, y_translation=0.0[, zoom=1.0]]]]]])
``````