from PIL import Image
import pygame
import sys
import time

colorzone = dict()

def img():
# 打开 BMP 文件
    bmp_image = Image.open('view.bmp')

    # 获取图像的基本信息
    width, height = bmp_image.size
    mode = bmp_image.mode

    print(f"图像宽度: {width}, 高度: {height}, 模式: {mode}")

    # 读取图像数据
    pixel_data = bmp_image.load()  # 获取像素数据

    # 遍历每个像素并打印其 RGB 值
    minleft = 0x3f3f3f3f
    mintop = 0x3f3f3f3f
    for y in range(height):
        for x in range(width):
            minleft = min(minleft, x)
            mintop = min(mintop, y)
            r, g, b = pixel_data[x, y]  # 获取每个像素的 RGB 值
            if r == 255 and b == 255 and g == 255:
                pass
            else:
                if (r,g,b) in colorzone:
                    colorzone[(r,g,b)].append(((x//8)*4,(y//8)*4))
                else:
                    colorzone[(r,g,b)] = list()
                    colorzone[(r,g,b)].append((x//8*4,y//8*4))
                #print(f"像素 ({x}, {y}): R={r}, G={g}, B={b}")

    colorzone[(255,255,255)] = colorzone[(0,0,0)]
    # 关闭图像
    bmp_image.close()

def grap():
    pygame.init()
    width, height = 800, 1000
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption("TEST")
    global colorzone
    while True:
        for i in colorzone.keys():    
            for j in colorzone[i]:
                if i != (255,255,255):
                    pygame.draw.rect(screen, i, (j[0], j[1], 2, 2))  # 远程波形
                else:
                    pygame.draw.rect(screen, i, (j[0], j[1], 1, 1))  # 远程波形
        pygame.display.flip()
        pygame.time.delay(10)

if __name__ == "__main__":
    print("START")
    img()
    print("LOADED")
    grap()