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

micropython解码bmp

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


分享给朋友:

相关文章

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

python利用pyinstaller打包简明教程

python利用pyinstaller打包简明教程

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

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

micropython 清除flash磁盘内容

micropython 清除flash磁盘内容

对于ESP32,一般是在命令行中使用esptool.py清除flash,如果是在程序中想清除flash内容,或者更换文件系统格式,可以用下面方法:import uos uos.VfsFat.mkfs(bdev)如果使用 LFS2 文件系统,命令是import uos uos.VfsLfs2.mkfs(bdev)对于STM32,默认没有bdev设备,需要使用下面方法:import uos flash = pyb.Flash(start=0)u...

word转换服务python版本

word转换服务python版本

网页无法打开word文档。但是可以转换为pdf来在网页上找开。用python实现在线转换服务。from flask import Flask, request, jsonify,redirect,send_from_directory from win32com.client import constants,gencache import requests import os impor...

Python内置函数

Python内置函数

以下是 Python 中的全部内置函数,并列出了它们的作用和参数说明。请注意,以下列出的内置函数是基于 Python 3.9 版本。abs(): 返回一个数的绝对值。参数:abs(x)all(): 如果可迭代对象中的所有元素都为 True,则返回 True。参数:all(iterable)any(): 如果可迭代对象中的任何元素为 True,则返回 True。参数:any(iterable)ascii(): 返回一个包含 ASCII 转义字符的字符串表示。参数:ascii(object)bin(...

发表评论

访客

看不清,换一张

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