当前位置:首页 > 技术 > Python > 正文内容

micropython解码bmp

watrt3年前 (2021-02-19)Python15030
from ST7735 import TFT,TFTColor
from machine import SPI,Pin
spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
tft=TFT(spi,16,17,18)
tft.initr()
tft.rgb(True)
tft.fill(TFT.BLACK)

f=open('test128x160.bmp', 'rb')
if f.read(2) == b'BM':  #header
    dummy = f.read(8) #file size(4), creator bytes(4)
    offset = int.from_bytes(f.read(4), 'little')
    hdrsize = int.from_bytes(f.read(4), 'little')
    width = int.from_bytes(f.read(4), 'little')
    height = int.from_bytes(f.read(4), 'little')
    if int.from_bytes(f.read(2), 'little') == 1: #planes must be 1
        depth = int.from_bytes(f.read(2), 'little')
        if depth == 24 and int.from_bytes(f.read(4), 'little') == 0:#compress method == uncompressed
            print("Image size:", width, "x", height)
            rowsize = (width * 3 + 3) & ~3
            if height < 0:
                height = -height
                flip = False
            else:
                flip = True
            w, h = width, height
            if w > 128: w = 128
            if h > 160: h = 160
            tft._setwindowloc((0,0),(w - 1,h - 1))
            for row in range(h):
                if flip:
                    pos = offset + (height - 1 - row) * rowsize
                else:
                    pos = offset + row * rowsize
                if f.tell() != pos:
                    dummy = f.seek(pos)
                for col in range(w):
                    bgr = f.read(3)
                    tft._pushcolor(TFTColor(bgr[2],bgr[1],bgr[0]))
spi.deinit()


分享给朋友:

相关文章

esp32外部中断学习笔记

esp32外部中断学习笔记

思路:我使用的esp32开发板为简易的开发板,因为板上没有用户自己定义的键盘所以在使用中想要输入或者控制无法完成。研究发现在开发板上有的下载按钮是不是可以在进入系统后复用为一个按键呢。实现:参考官方的例程对代码如下首先宏定义参数:#define KYE_0   0    //GPIO0检测 #define KEYS_SET   (1ULL<<0) &nb...

python编写的简单http请求和应答

python编写的简单http请求和应答

响应(服务端)import machine import socket pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 2, 4, 5, 12, 13, 14, 15)] adc = machine.ADC(0) html = &quo...

micropython中使用rtc设置时间

micropython中使用rtc设置时间

micropython中使用rtc设置时间from machine import RTC rtc = RTC() rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time print(rtc.datetime(...

micropython 使用ili9163显示IP地址

micropython 使用ili9163显示IP地址

mian.pyexec(open('./wifi.py').read(),globals()) exec(open('./tft.py').read(),globals())wifi.pyimport network import machine import ntptime import time nic = network.WLAN(network.STA_IF) # c...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。