树莓派pico显示图片

树莓派pico显示图片

20240329145655678-232428rxx4okgh2o3co4i1

目标

在树莓派pico上显示以上图片

准备

  • 一个安装有python的电脑
  • 连接好屏幕的树莓派pico
  • 脑子若干

安装OpenCV库

本地电脑安装opencv(注意,是电脑,不是树莓派pico)

pip install opencv-python

图片转bmp

下载图片转bmp格式工具

20240329150459652-image

图片二值化

电脑代码运行以下代码

import cv2 as cv
import numpy as np
def binary_image(image):
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
    h, w =gray.shape[:2]
    m = np.reshape(gray, [1,w*h])
    mean = m.sum()/(w*h)
    _, binary =  cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
    return binary


image = cv.imread("123.bmp")
image = binary_image(image)#二值化
cv.imwrite('123.pbm',image)#保存
with open('123.pbm', 'rb') as f:  # 注意,这里的'rb'参数很重要,没有的话会读取错误
    f.readline()

    data = bytearray(f.read())
print(data)

运行结果(如果显示有误,就把运行结果最前面的61 64\n删除

bytearray(b'61 64\n\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x07\xf0\x00\x00\x00\x03\xe3\xe0?\xbe\x00\x00\x00\x0e@0`\x07\x80\x00\x00\x0c\x00\x18\x80\x01\x80\x00\x00\x0c\x00\r\x80\x01\x80\x00\x00\x0c\x10\x05\x00A\x80\x00\x00\x0e\x04\x07\x01\x03\x80\x00\x00\x06\x01\x07\x04\x01\x00\x00\x00\x06\x00\x87\x18\x01\x00\x00\x00\x03\x00o\xa0\x06\x00\x00\x00\x02\x00?\xe0\x06\x00\x00\x00\x01\x80\x1f\xc0\x0c\x00\x00\x00\x01\xc0?\xe0\x18\x00\x00\x00\x00x\xff\xf0\xf0\x00\x00\x00\x00\x7f\xff\xff\xe0\x00\x00\x00\x00x\xe0?\xe0\x00\x00\x00\x00`\xc0\x10x\x00\x00\x00\x01\x81\x80\x08\x18\x00\x00\x00\x01\x83\x80\x0c\x1c\x00\x00\x00\x01\x07\xc0\x1e\x0c\x00\x00\x00\x03\x0f\xf0\x7f\x0e\x00\x00\x00\x03>\x1f\xc1\xc6\x00\x00\x00\x03x\x0f\x80\xe6\x00\x00\x00\x07\xf0\x07\x00~\x00\x00\x00\x07\xe0\x07\x00?\x00\x00\x00\x18\xe0\x07\x001\x80\x00\x00\x18\xc0\x07\x00\x11\xc0\x00\x00\x10\xc0\x07\x00\x10\xc0\x00\x00\x10\xc0\x07\x00\x10`\x00\x000\xc0\x0f\x80\x10`\x00\x000\xe0\x0f\x800`\x00\x000\xe0\x1f\xe0x\xe0\x00\x00\x11\xf00p\xf8\xc0\x00\x00\x19\xff\xc0\x1f\xf9\xc0\x00\x00\x19\xff\xc0\x1f\xc5\x80\x00\x00\x0f\x1f\x80\x0f\x83\x00\x00\x00\x06\x0f\x80\x0f\x03\x00\x00\x00\x06\x07\x80\x06\x03\x00\x00\x00\x06\x03\x80\x0c\x02\x00\x00\x00\x06\x01\x80\x0c\x02\x00\x00\x00\x06\x01\xc0\x18\x06\x00\x00\x00\x03\x01\xe08\x06\x00\x00\x00\x01\x81\xf0x\x0c\x00\x00\x00\x01\xc1\xff\xf8\x18\x00\x00\x00\x00\xe1\xff\xfc8\x00\x00\x00\x00?\xe0\x1f\xe0\x00\x00\x00\x00\x1f\x80\x0f\x80\x00\x00\x00\x00\x07\x80\x0f\x00\x00\x00\x00\x00\x03\xc0\x1c\x00\x00\x00\x00\x00\x00`p\x00\x00\x00\x00\x00\x00\x7f\xe0\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

20240329150722206-image

pico运行显示图像代码

ssd1306显示图片

from machine import Pin, I2C
import framebuf
i2c=I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
# i2c=I2C(1, scl=Pin(19),sda=Pin(18), freq=100000)
from ssd1306 import SSD1306_I2C
oled = SSD1306_I2C(128, 32, i2c)                 # Init oled display

data = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x07\xf0\x00\x00\x00\x03\xe3\xe0?\xbe\x00\x00\x00\x0e@0`\x07\x80\x00\x00\x0c\x00\x18\x80\x01\x80\x00\x00\x0c\x00\r\x80\x01\x80\x00\x00\x0c\x10\x05\x00A\x80\x00\x00\x0e\x04\x07\x01\x03\x80\x00\x00\x06\x01\x07\x04\x01\x00\x00\x00\x06\x00\x87\x18\x01\x00\x00\x00\x03\x00o\xa0\x06\x00\x00\x00\x02\x00?\xe0\x06\x00\x00\x00\x01\x80\x1f\xc0\x0c\x00\x00\x00\x01\xc0?\xe0\x18\x00\x00\x00\x00x\xff\xf0\xf0\x00\x00\x00\x00\x7f\xff\xff\xe0\x00\x00\x00\x00x\xe0?\xe0\x00\x00\x00\x00`\xc0\x10x\x00\x00\x00\x01\x81\x80\x08\x18\x00\x00\x00\x01\x83\x80\x0c\x1c\x00\x00\x00\x01\x07\xc0\x1e\x0c\x00\x00\x00\x03\x0f\xf0\x7f\x0e\x00\x00\x00\x03>\x1f\xc1\xc6\x00\x00\x00\x03x\x0f\x80\xe6\x00\x00\x00\x07\xf0\x07\x00~\x00\x00\x00\x07\xe0\x07\x00?\x00\x00\x00\x18\xe0\x07\x001\x80\x00\x00\x18\xc0\x07\x00\x11\xc0\x00\x00\x10\xc0\x07\x00\x10\xc0\x00\x00\x10\xc0\x07\x00\x10`\x00\x000\xc0\x0f\x80\x10`\x00\x000\xe0\x0f\x800`\x00\x000\xe0\x1f\xe0x\xe0\x00\x00\x11\xf00p\xf8\xc0\x00\x00\x19\xff\xc0\x1f\xf9\xc0\x00\x00\x19\xff\xc0\x1f\xc5\x80\x00\x00\x0f\x1f\x80\x0f\x83\x00\x00\x00\x06\x0f\x80\x0f\x03\x00\x00\x00\x06\x07\x80\x06\x03\x00\x00\x00\x06\x03\x80\x0c\x02\x00\x00\x00\x06\x01\x80\x0c\x02\x00\x00\x00\x06\x01\xc0\x18\x06\x00\x00\x00\x03\x01\xe08\x06\x00\x00\x00\x01\x81\xf0x\x0c\x00\x00\x00\x01\xc1\xff\xf8\x18\x00\x00\x00\x00\xe1\xff\xfc8\x00\x00\x00\x00?\xe0\x1f\xe0\x00\x00\x00\x00\x1f\x80\x0f\x80\x00\x00\x00\x00\x07\x80\x0f\x00\x00\x00\x00\x00\x03\xc0\x1c\x00\x00\x00\x00\x00\x00`p\x00\x00\x00\x00\x00\x00\x7f\xe0\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')




buf = framebuf.FrameBuffer(data, 32, 32, framebuf.MONO_HLSB)
oled.blit(buf, 0, 0)
oled.show()

SH1106显示图片

from machine import Pin, I2C, ADC
from sh1106 import SH1106_I2C
import framebuf
WIDTH  = 128                                            # oled display width
HEIGHT = 64                                             # oled display height
i2c = I2C(0, scl=Pin(21),sda=Pin(20),freq=400000)
                  # Display I2C config
oled = SH1106_I2C(WIDTH, HEIGHT, i2c)  
data = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x07\xf0\x00\x00\x00\x03\xe3\xe0?\xbe\x00\x00\x00\x0e@0`\x07\x80\x00\x00\x0c\x00\x18\x80\x01\x80\x00\x00\x0c\x00\r\x80\x01\x80\x00\x00\x0c\x10\x05\x00A\x80\x00\x00\x0e\x04\x07\x01\x03\x80\x00\x00\x06\x01\x07\x04\x01\x00\x00\x00\x06\x00\x87\x18\x01\x00\x00\x00\x03\x00o\xa0\x06\x00\x00\x00\x02\x00?\xe0\x06\x00\x00\x00\x01\x80\x1f\xc0\x0c\x00\x00\x00\x01\xc0?\xe0\x18\x00\x00\x00\x00x\xff\xf0\xf0\x00\x00\x00\x00\x7f\xff\xff\xe0\x00\x00\x00\x00x\xe0?\xe0\x00\x00\x00\x00`\xc0\x10x\x00\x00\x00\x01\x81\x80\x08\x18\x00\x00\x00\x01\x83\x80\x0c\x1c\x00\x00\x00\x01\x07\xc0\x1e\x0c\x00\x00\x00\x03\x0f\xf0\x7f\x0e\x00\x00\x00\x03>\x1f\xc1\xc6\x00\x00\x00\x03x\x0f\x80\xe6\x00\x00\x00\x07\xf0\x07\x00~\x00\x00\x00\x07\xe0\x07\x00?\x00\x00\x00\x18\xe0\x07\x001\x80\x00\x00\x18\xc0\x07\x00\x11\xc0\x00\x00\x10\xc0\x07\x00\x10\xc0\x00\x00\x10\xc0\x07\x00\x10`\x00\x000\xc0\x0f\x80\x10`\x00\x000\xe0\x0f\x800`\x00\x000\xe0\x1f\xe0x\xe0\x00\x00\x11\xf00p\xf8\xc0\x00\x00\x19\xff\xc0\x1f\xf9\xc0\x00\x00\x19\xff\xc0\x1f\xc5\x80\x00\x00\x0f\x1f\x80\x0f\x83\x00\x00\x00\x06\x0f\x80\x0f\x03\x00\x00\x00\x06\x07\x80\x06\x03\x00\x00\x00\x06\x03\x80\x0c\x02\x00\x00\x00\x06\x01\x80\x0c\x02\x00\x00\x00\x06\x01\xc0\x18\x06\x00\x00\x00\x03\x01\xe08\x06\x00\x00\x00\x01\x81\xf0x\x0c\x00\x00\x00\x01\xc1\xff\xf8\x18\x00\x00\x00\x00\xe1\xff\xfc8\x00\x00\x00\x00?\xe0\x1f\xe0\x00\x00\x00\x00\x1f\x80\x0f\x80\x00\x00\x00\x00\x07\x80\x0f\x00\x00\x00\x00\x00\x03\xc0\x1c\x00\x00\x00\x00\x00\x00`p\x00\x00\x00\x00\x00\x00\x7f\xe0\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')




buf = framebuf.FrameBuffer(data, 60, 64, framebuf.MONO_HLSB)
oled.blit(buf, 0, 0)
oled.show()

20240329152510507-image

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容