例程讲解-03-image_drawing_with_custom_palette 使用用户自定义调色板绘制图像

本例程为03-image_drawing_with_custom_palette.py

# 用自定义调色板绘制图像示例
#
# 这个例子展示了如何使用自定义生成的调色板在帧缓冲区中绘制图像

import sensor, image, time, pyb

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # or GRAYSCALE...
sensor.set_framesize(sensor.QQVGA) # or QQVGA...
sensor.skip_frames(time = 2000)
clock = time.clock()

# 调色板实际上是一个图像,这允许您使用图像操作来创建调色板
# 图像必须有256个条目,即256x1、64x4、16x16,格式为rgb565

# 初始化调色板源颜色到一个图像
palette_source_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 0, 255)]
palette_source_color_image = image.Image(len(palette_source_colors), 1, sensor.RGB565)
for i, color in enumerate(palette_source_colors):
    palette_source_color_image[i] = color

# 将图像缩放到调色板宽度并使其平滑
palette = image.Image(256,1, sensor.RGB565)
palette.draw_image(palette_source_color_image, 0, 0, x_scale=palette.width() / palette_source_color_image.width())
palette.mean(int(palette.width() / palette_source_color_image.width()/2))

while(True):
    clock.tick()

    img = sensor.snapshot()
    # Get a copy of grayscale image before converting to color
    # 在转换为颜色之前,获取一份灰度图像的拷贝
    img_copy = img.copy()

    img.to_rgb565()

    palette_boundary_inset = int(sensor.width() / 40)
    palette_scale_x = (sensor.width() - palette_boundary_inset * 2) / palette.width()

    img.draw_image(img_copy, 0, 0, color_palette=palette)
    img.draw_image(palette, palette_boundary_inset, palette_boundary_inset, x_scale=palette_scale_x, y_scale=8)
    img.draw_rectangle(palette_boundary_inset, palette_boundary_inset, int(palette.width()*palette_scale_x), 8, color=(255,255,255), thickness=1)

    print(clock.fps())

results matching ""

    No results matching ""