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

micropython解码bmp

Watrt4年前 (2021-02-19)Python24620
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...

micropython 使用oled显示前面设置的时间。

micropython 使用oled显示前面设置的时间。

from machine import I2C, Pin import ssd1306,time    #这里引用的是官方的ssd1306库。可以在github上下载下来放入库中。 i2c = I2C(-1, Pin(14), Pin(2)) display = ssd1306.SSD1306_I2C(128, 64, i...

micropython连接到wifi上面

micropython连接到wifi上面

import network,time,ujson,urequests,ntptime from machine import RTC nic=network.WLAN(network.STA_IF) nic.active(True) #nic.connect('ZCWH','00004157') nic.connect('028sd.com','88884444') nic.co...

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...

python利用pyinstaller打包简明教程

python利用pyinstaller打包简明教程

在创建了独立应用(自包含该应用的依赖包)之后,还可以使用 PyInstaller 将 Python 程序生成可直接运行的程序,这个程序就可以被分发到对应的 Windows 或 Mac OS X 平台上运行。安装 PyInstallePython 默认并不包含 PyInstaller 模块,因此需要自行安装 PyInstaller 模块。安装 PyInstaller 模块与安装其他 Python 模块一样,使用 pip 命令安装即可。在命令行输入如下命令:pip install ...

用mpy-cross保护py文件

用mpy-cross保护py文件

在python中,可以将py文件编译为pyc文件。编译后的pyc文件是二进制格式,一是可以加快加载速度,更重要的是可以保护原始代码。在micropython中同样提供了这个功能,只是它将pyc改名为mpy,编译出的文件扩展名是.mpy。要使用这个功能,首先,我们需要产生mpy-cross工具。在micropython目录下,进入mpy-cross子目录,然后在命令行下输入make编译产生mpy-cross执行文件(需要先安装gcc编译器)。在windows会产生mpy-cross.exe,在li...

Python使用struct处理二进制(pack和unpack用法)

Python使用struct处理二进制(pack和unpack用法)

有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重要的三个函数是pack(), unpack(), calcsize()pack(fmt, v1, v2, ...)     按照给定的格式(fmt),把数据封装成字符串(实际上是类似于c结构体的字节流) un...

asyncio --- 异步 I/O http服务代码

asyncio --- 异步 I/O http服务代码

import asyncioasync def service(reader,writer):    data = await reader.read(1024) #同步读取数据    data =data.decode().replace('\r','<br/>')    print(writer.get_extra_info('peername'))   &nbs...

发表评论

访客

看不清,换一张

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