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

micropython解码bmp

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


打赏 支付宝打赏 微信打赏
分享给朋友:

相关文章

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

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

pyqt5 开发入门

pyqt5 开发入门

使用:pyqt5-tools designer 进行窗口设计使用命令:pyuic5 -o textedit.py textedit.ui  把设计的UI文件转换成py文件注意:使用对话框的话要继承于class Ui_MainWindow(QtWidgets.QMainWindow):使用信号的方法:self.actionnew.triggered.connect(self.new_btn)    self.new_btn 为当前类下的方法下面示例:#&nb...

micropython 1.17编译出现FAILED: esp-idf/mbedtls/x509_crt_bundle

micropython 1.17编译出现FAILED: esp-idf/mbedtls/x509_crt_bundle

micropython 1.17编译出现FAILED: esp-idf/mbedtls/x509_crt_bundle 错误解决方法。出现错误的原因是ssl中的证书过期了。解决办法:1:(Top) > Component config > mbedTLS > Certificate Bundle->Enable trusted root certificate bundle   禁用2:修改配置文件 :sdkconfig.base  中添加入...

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

发表评论

访客

看不清,换一张

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