1、引入模块
from PIL import ImageDraw
from PIL import Image
from PIL import ImageFont
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
2、一些默认参数
# Raspberry Pi pin configuration:
RST = 24
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
2、获取OLED的实例
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
3、将oled库初始化,并且清除界面
disp.begin()
disp.clear()
disp.display()
4、创建一幅新的图片,图片大小为oled的尺寸
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
5、将图片加载在绘制对象上,相当于加载在画板上
draw = ImageDraw.Draw(image)
6、字体选择
# 库里默认的字体,在ImageFont里面
default_font = ImageFont.load_default()
# 树莓派里的字体库,可以设置字体大小
font = ImageFont.truetype('/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf', 29)
7、绘制文字
draw.text((0, 40), "hello world", font=font, fill=255)
8、在oled显示
disp.image(image)
disp.display()
补充:
树莓派上中文字库下载:sudo apt-get install ttf-wqy-zenhei