Change example to generate a palette rather than use pre-created

Also changed to sho the palette at top of screen.
This script wont work until the fixes to filters with y_size < k_size are merged. #764
This commit is contained in:
mattdawson 2020-04-24 18:34:27 +12:00
parent eb94f153dd
commit c4bcfd80e4

View File

@ -1,6 +1,6 @@
# Draw Image Example with custom color palette # Draw Image Example with custom color palette
# #
# This example shows off how to draw images in the frame buffer with a custom color palette. # This example shows off how to draw images in the frame buffer with a custom generated color palette.
import sensor, image, time, pyb import sensor, image, time, pyb
@ -12,19 +12,32 @@ clock = time.clock()
# the color palette is actually an image, this allows you to use image ops to create palettes # 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 # the image must have 256 entries i.e. 256x1, 64x4, 16x16 and have the format rgb565
# Initialise palette source colors into an image
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
# Scale the image to palette width and smooth them
palette = image.Image(256,1, sensor.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)] palette.draw_image(palette_source_color_image, 0, 0, x_scale=palette.width() / palette_source_color_image.width())
for i in range(0,256): palette.mean(int(palette.width() / palette_source_color_image.width()/2))
palette[i] = funky_palette[i]
while(True): while(True):
clock.tick() clock.tick()
img = sensor.snapshot() img = sensor.snapshot()
# Get a copy of grayscale image before converting to color
img_copy = img.copy() img_copy = img.copy()
img.to_rgb565() 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(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()) print(clock.fps())