mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
34 lines
4.7 KiB
Python
34 lines
4.7 KiB
Python
# Draw Image Example with custom color palette
|
|
#
|
|
# This example shows off how to draw images in the frame buffer with a custom color palette.
|
|
|
|
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()
|
|
|
|
# the color palette is actually an image, this allows you to use image ops to create palettes
|
|
# the image must have 256 entries i.e. 256x1, 64x4, 16x16 and have the format rgb565
|
|
palette = image.Image(256,1, sensor.RGB565)
|
|
funky_palette = [(0, 0, 0), (255, 255, 247), (255, 255, 247), (255, 255, 239), (255, 255, 239), (255, 255, 230), (255, 255, 230), (255, 251, 214), (255, 251, 214), (255, 251, 206), (255, 251, 206), (255, 247, 189), (255, 247, 189), (255, 247, 181), (255, 247, 181), (255, 247, 165), (255, 247, 165), (255, 247, 165), (255, 243, 148), (255, 243, 148), (255, 243, 132), (255, 243, 132), (255, 243, 115), (255, 243, 115), (255, 239, 99), (255, 239, 99), (255, 239, 90), (255, 239, 90), (255, 235, 74), (255, 235, 74), (255, 231, 66), (255, 231, 66), (255, 231, 49), (255, 231, 49), (255, 231, 49), (255, 227, 41), (255, 227, 41), (255, 223, 33), (255, 223, 33), (255, 223, 25), (255, 223, 25), (255, 219, 16), (255, 219, 16), (255, 215, 8), (255, 215, 8), (255, 210, 8), (255, 210, 8), (255, 206, 8), (255, 206, 8), (255, 206, 0), (255, 206, 0), (255, 206, 0), (255, 202, 0), (255, 202, 0), (255, 198, 0), (255, 198, 0), (255, 194, 0), (255, 194, 0), (255, 190, 0), (255, 190, 0), (255, 186, 0), (255, 186, 0), (255, 182, 0), (255, 182, 0), (255, 178, 0), (255, 178, 0), (255, 174, 0), (255, 174, 0), (255, 174, 0), (255, 170, 0), (255, 170, 0), (255, 170, 0), (255, 170, 0), (255, 162, 0), (255, 162, 0), (255, 158, 0), (255, 158, 0), (255, 154, 0), (255, 154, 0), (247, 150, 0), (247, 150, 0), (247, 146, 0), (247, 146, 0), (247, 142, 0), (247, 142, 0), (247, 142, 0), (247, 138, 0), (247, 138, 0), (247, 138, 0), (247, 138, 0), (247, 134, 0), (247, 134, 0), (247, 130, 0), (247, 130, 0), (247, 125, 0), (247, 125, 0), (247, 121, 0), (247, 121, 0), (247, 117, 0), (247, 117, 0), (247, 113, 0), (247, 113, 0), (247, 113, 0), (239, 109, 0), (239, 109, 0), (239, 105, 0), (239, 105, 0), (239, 101, 0), (239, 101, 0), (239, 101, 0), (239, 101, 0), (239, 97, 0), (239, 97, 0), (239, 93, 8), (239, 93, 8), (239, 89, 8), (239, 89, 8), (239, 85, 8), (239, 85, 8), (239, 85, 8), (239, 81, 8), (239, 81, 8), (230, 77, 8), (230, 77, 8), (230, 77, 16), (230, 77, 16), (230, 73, 16), (230, 73, 16), (230, 69, 25), (230, 69, 25), (230, 65, 33), (230, 65, 33), (222, 61, 41), (222, 61, 41), (222, 57, 58), (222, 57, 58), (222, 57, 58), (222, 53, 66), (222, 53, 66), (222, 49, 74), (222, 49, 74), (222, 45, 90), (222, 45, 90), (214, 40, 99), (214, 40, 99), (214, 36, 107), (214, 36, 107), (214, 32, 107), (214, 32, 107), (214, 28, 115), (214, 28, 115), (206, 24, 123), (206, 24, 123), (206, 24, 123), (206, 24, 123), (206, 24, 123), (206, 20, 132), (206, 20, 132), (197, 16, 132), (197, 16, 132), (197, 12, 140), (197, 12, 140), (197, 12, 140), (197, 12, 140), (189, 8, 148), (189, 8, 148), (189, 4, 148), (189, 4, 148), (189, 4, 148), (189, 4, 148), (189, 4, 148), (181, 4, 148), (181, 4, 148), (181, 0, 148), (181, 0, 148), (181, 0, 148), (181, 0, 148), (173, 0, 148), (173, 0, 148), (173, 0, 156), (173, 0, 156), (165, 0, 156), (165, 0, 156), (165, 0, 156), (165, 0, 156), (156, 0, 156), (156, 0, 156), (156, 0, 156), (148, 0, 156), (148, 0, 156), (148, 0, 156), (148, 0, 156), (140, 0, 156), (140, 0, 156), (132, 0, 156), (132, 0, 156), (132, 0, 156), (132, 0, 156), (123, 0, 156), (123, 0, 156), (115, 0, 156), (115, 0, 156), (107, 0, 156), (107, 0, 156), (107, 0, 156), (107, 0, 156), (107, 0, 156), (99, 0, 156), (99, 0, 156), (90, 0, 156), (90, 0, 156), (82, 0, 148), (82, 0, 148), (74, 0, 148), (74, 0, 148), (74, 0, 148), (74, 0, 148), (66, 0, 148), (66, 0, 148), (58, 0, 148), (58, 0, 148), (58, 0, 148), (58, 0, 140), (58, 0, 140), (49, 0, 140), (49, 0, 140), (41, 0, 140), (41, 0, 140), (33, 0, 132), (33, 0, 132), (25, 0, 132), (25, 0, 132), (16, 0, 123), (16, 0, 123), (16, 0, 115), (16, 0, 115), (8, 0, 115), (8, 0, 115), (8, 0, 115), (8, 0, 107), (8, 0, 107), (0, 0, 99), (0, 0, 99), (0, 0, 90), (0, 0, 90), (0, 0, 82), (0, 0, 82), (0, 0, 66), (0, 0, 66), (0, 0, 49), (0, 0, 49), (0, 0, 33), (0, 0, 33), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
|
|
for i in range(0,256):
|
|
palette[i] = funky_palette[i]
|
|
|
|
while(True):
|
|
clock.tick()
|
|
|
|
img = sensor.snapshot()
|
|
small_img = img.mean_pooled(4, 4) # Makes a copy.
|
|
|
|
img.to_rgb565()
|
|
|
|
x = (img.width()//2)-(small_img.width()//2)
|
|
y = (img.height()//2)-(small_img.height()//2)
|
|
|
|
img.draw_image(small_img, x, y, x_scale=2, y_scale=2, color_palette=palette)
|
|
|
|
print(clock.fps())
|