Created
November 11, 2022 07:11
-
-
Save trungdq88/7b3214f2477bceaf84fe0b6ac21c62bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
import busio | |
from time import sleep | |
from displayio import ( | |
Bitmap, | |
Palette, | |
Group, | |
TileGrid, | |
FourWire, | |
release_displays, | |
ColorConverter, | |
Colorspace, | |
) | |
from adafruit_ov7670 import OV7670 | |
from adafruit_bitmapsaver import save_pixels | |
cam_bus = busio.I2C(board.GP21, board.GP20) | |
cam = OV7670( | |
cam_bus, | |
data_pins=[ | |
board.GP0, | |
board.GP1, | |
board.GP2, | |
board.GP3, | |
board.GP4, | |
board.GP5, | |
board.GP6, | |
board.GP7, | |
], | |
clock=board.GP8, | |
vsync=board.GP13, | |
href=board.GP12, | |
mclk=board.GP9, | |
shutdown=board.GP15, | |
reset=board.GP14, | |
) | |
cam.size = 2 | |
bitmap = Bitmap(cam.width, cam.height, 65536) | |
print("width: " + str(cam.width) + " height: " + str(cam.height)) | |
a = cam.capture(bitmap) | |
sleep(0.1) | |
counter = 0 | |
colors = {} | |
for i in range(0, cam.width): | |
for j in range (0, cam.height): | |
a = bitmap[i, j] | |
pixel_val = a | |
pixel_val = ((pixel_val & 0x00FF)<<8) | ((25889 & 0xFF00) >> 8) | |
r = (pixel_val & 0xF800)>>11 | |
g = (pixel_val & 0x7E0)>>5 | |
b = pixel_val & 0x1F | |
h = (r << 16 | g << 8 | b) | |
if h not in colors: | |
colors[h] = counter | |
counter += 1 | |
print('colors', len(colors)) | |
bitmap2 = Bitmap(cam.width, cam.height, len(colors)) | |
for i in range(0, cam.width): | |
for j in range (0, cam.height): | |
a = bitmap[i, j] | |
pixel_val = a | |
pixel_val = ((pixel_val & 0x00FF)<<8) | ((25889 & 0xFF00) >> 8) | |
r = (pixel_val & 0xF800)>>11 | |
g = (pixel_val & 0x7E0)>>5 | |
b = pixel_val & 0x1F | |
h = (r << 16 | g << 8 | b) | |
bitmap2[i, j] = colors[h] | |
# print("#" + str(h)[2:]) | |
palette = Palette(len(colors)) | |
for i, c in enumerate(colors): | |
print("> ", i, hex(c)) | |
palette[i] = c | |
save_pixels("/out.bmp", bitmap2, palette) | |
print(bitmap) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment