Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython esp32 ssd1306 image to pbm


ssd1306可以说是市面上最常见的oled屏了,单色,128x64分辨率,驱动大把大把,今天主要研究怎么显示图片。
一般默认驱动是不带显示图片功能的,我们使用frambuf函数来显示,创建图像显示区域,再显示即可

imagebuf=framebuf.FrameBuffer(bytearray(b’’)’),84,64,framebuf.MONO_HLSB)
oled.blit(imagebuf,0,0)
oled.show()

像上面这样基本ok了,那么如果使用的图片较多,有没有批量的方法呢?
我特意写了一个小工具用于批量转换

Snipaste_2022-07-26_01-01-24.jpg


使用方法:
将transpic.py和所有图片素材放在同一文件夹下,运行transpic.py就会自动转换所有图片文件(需要pc python 的PIL库!!!),如果嫌使用pbm文件麻烦,也生成了一个buf.txt,直接去里面粘贴代码也可以。
要注意的是由于单色屏幕表现力很有限,所以复杂的图像会显示不好,所以尽量找一些简单的,对比度大的图片,转换后如果效果不满意也可以自己手动修正一下(反正分辨率不大,改起来没有那么麻烦)
转换工具在这里:



image to pbm

main.py ssd1306测试程序

transpic.py 将图片转成pbm文件和原始编码

图片文件转pbm原始编码,适用于单色屏。


main.py

from machine import I2C,Pin      
from ssd1306 import SSD1306_I2C    
import framebuf,time

i2c = I2C(sda=Pin(13), scl=Pin(14))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)

baozounew=framebuf.FrameBuffer(bytearray(b'\xf9\xfc\xfe~\x7f?\x9f\xcf\xcf\xe7\xf0\xf3\xfc\xfc\xfe~\x7f\x1f\x9f\xcf\xcf\xe0\xf3\xf9\xf8\xf8\xfc\x7f?\x1f\x1f\x8f\xe0\xe7\xf1\xf1\xf9\xf8\xfc~??\x1f\x80\xc7\xe3\xf3\xf1\xf9\xfc\xfe~??\x80\x9f\xe7\xe7\xe3\xe3\xf8\xfc\xfc~\x7f\x10\x9f\x8f\xc7\xe7\xf3\xf3\xf8\xfc\xfe~0?\x9f\x8f\x8f\xc7\xf1\xf1\xf1\xf8\xfep\x7f\x1f\x1f\x8f\x8f\xc7\xe3\xf1\xf1\xf8\xf0\x7f\x7f\xbf\xbf\xdf\xef\xef\xf7\xfb\xfc\xf0\xd5Uj\xb5UUUR\xaa\xab\xb0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xf8\x00\x03\xc0\x00\x00\x0f\xe0\x00\x00\x01\xff\xff\xff\xf0\x00\x00\x0f\xe0\x00\x00\x01\xff\xff\xff\xf8\x00\x00\x0f\xe0\x00\x00\x01\xf8\x00\x03\xf8\x00\x00\x0f\xc0\x00\x00\x01\xf8\x00\x03\xf0\x00\x00\x0f\xc0\x00\x00\x01\xff\xff\xff\xf0\x00\x00\x0f\xc00\x00\x01\xff\xff\xff\xf0\x00\x00\x0f\xc0|\x00\x03\xf0\x00\x03\xf0\x00\x00\x0f\xc0~\x00\x01\xfc\x00\x03\xf0\x01\xff\xff\xff\xff\x00\x01\xff\xff\xff\xf0\x01\xff\xff\xff\xff\x00\x03\xff\xff\xff\xe0\x00\x00\x0f\xc0\x00\x00\x01\xf8\x00\x00p\x00\x00\x0f\xc0\x00\x00\x01\xe7\xf0\xfe\x0f?\xb8<\x00}\xff\xc0\x00\x008\x07\xff\xbc\x0c\x00|\xff\xc0\x00\x00`\x0f\xff\xbf\x80\x00\xf9\xff\xc0\x00\x00\x07\xff\x1f\xbf\xc0\x01\xf8\xff\xc0\x00\x00\x07\xfc?\x9f\xe0\x01\xf0\x7f\xff\xff\xf0\x03\xfb\xff\x87\xf0\x03\xe0?\xff\xff\xc0\x03\xe1\xff\x87\xf0\x03\xc0?\xff\xff\xc0\x01\x80\xff\x81\xe0\x07\x00\x0f\xff\xff\x80\x01\x80\x7f\x00\xc0\x0e\x00\x03\xff\xff\x00\x00\x00~\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xf0\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xf0\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xbf\xb8c\x87\xf8x\x7f\x1b\x87?\x80\xb0\x18\xe7\xc6\x18x\xe3\x9b\xe6q\xc0\x9f\x9c\xc6\xcf\xd8\xd8\xc0\x9b\xe7a\xc0\xbf\xcd\xcc\xc7\xf9\xcc\xc0\x1b6c\xc0\xb0\r\x8f\xe6\x1c\xfe\xc1\x9b\x9fa\xc0\xbf\x07\x9e\xf69\xc6s\x9b\x9eu\xc0\x9f\xc7\x187\xfb\x87\x7f\x1b\x07\x7f@\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xf0'),84,64,framebuf.MONO_HLSB)
oled.blit(baozounew,20,0)

oled.show()


transpic.py

from PIL import Image
import os

file_dir=os.getcwd()
filelist=[]
buflist=[]

#转换图像文件为pbm
for root, dirs, files in os.walk(file_dir): 
    for filename in files:
        try:
            print(filename)
            im=Image.open(filename)
            im=im.convert('1')
            im.save(filename[0:filename.find('.')]+'.pbm')
        except:
            pass

#读出位图数据
for root, dirs, files in os.walk(file_dir):
    pbmfile= [f for f in files if f.endswith(".pbm")]
    for filename in pbmfile:
        with open(filename,'rb') as f:
            f.readline()
            width,height=[int(v) for v in f.readline().split()]
            data=bytearray(f.read())
            #print(data)
            #print(width,height)
            f.close()
        logobuf=filename[:-4]+'=framebuf.FrameBuffer('+str(data)+','+str(width)+','+str(height)+',framebuf.MONO_HLSB)'
        buflist.append(logobuf)

#写入txt文件
with open("buf.txt","w") as f:
    for buf in buflist:
        f.write(buf)
        f.write('\r')
    f.close()


来源:https://blog.csdn.net/jd3096/article/details/121319042


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0092999935150146 seconds