2 回答

TA貢獻1853條經(jīng)驗 獲得超18個贊
我沒有 SH1106 可供測試,也從未使用過該luma
庫,因此可能有一種更簡單的方法可以完成您想要的操作。如果是這樣,也許有人會好心地告訴我,我會刪除這個答案。
background = Image.new("RGB", device.size, "white")
background.paste(frame.resize(size, resample=Image.LANCZOS), posn)
device.display(background.convert(device.mode))
所以,看來你可以創(chuàng)建一個PIL Image并將其發(fā)送到顯示器上。第一行創(chuàng)建一個與整個顯示器大小相同的空白白色畫布,第二行將另一個畫布粘貼PIL Image到指定位置的畫布上,最后一行將圖像發(fā)送到顯示器。因此,您需要做的就是分別定義您的N “區(qū)域”并分別繪制它們(每個區(qū)域都是一個PIL Image),然后當(dāng)您想要更新顯示時,將您的N區(qū)域粘貼到您想要的位置并發(fā)送完整的圖片到顯示屏。
抱歉,我不能更準(zhǔn)確,但我沒有什么可以測試的。這是一個包含 3 個區(qū)域的小示例,可以單獨繪制這些區(qū)域,然后在調(diào)用之前將其組裝成一個整體device.display()
#!/usr/bin/env python3
from PIL import Image, ImageDraw
def UpdateDisplay(z1,z2,z3):
? ?"""Pass in the three zones and they will be sent to the screen"""
? ?# Make a black canvas the size of the entire screen
? ?whole = Image.new("RGB", (128,64), (0,0,0))
? ?# Now paste in the 3 zones to form the whole
? ?whole.paste(z1, (2,2))? ? ? ? # zone1 at top-left
? ?whole.paste(z2, (66,2))? ? ? ?# zone2 at top-right
? ?whole.paste(z3, (2,34))? ? ? ?# zone3 across the bottom
? ?# I save the image here, but you would write it to the screen with "device.display()"
? ?whole.save('result.png')
? ?return
# Make zone1 dark grey and annotate it
z1 = Image.new("RGB", (60,30), (64,64,64))
z1draw = ImageDraw.Draw(z1)
z1draw.text((10,10),"Zone1")
# Make zone2 mid-grey and annotate it
z2 = Image.new("RGB", (60,30), (128,128,128))
z2draw = ImageDraw.Draw(z2)
z2draw.text((10,10),"Zone2")
# Make zone3 light grey and annotate it
z3 = Image.new("RGB", (124,28), (192,192,192))
z3draw = ImageDraw.Draw(z3)
z3draw.text((10,10),"Zone3")
# Blit all zones to display
UpdateDisplay(z1,z2,z3)
# Now change just zone 2 and update display
z2.paste("red", (0,0,z2.width,z2.height))
UpdateDisplay(z1,z2,z3)
這是原始顯示:
更新 zone2 后再次出現(xiàn):

TA貢獻1833條經(jīng)驗 獲得超4個贊
好吧,我基本上明白了:
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import sh1106
from PIL import ImageFont, Image, ImageDraw
### setting up display using LUMA oled
device = sh1106(i2c(port=1, address=0x3C), rotate=0)
device.clear()
### Initialize drawing zone (aka entire screen)
output = Image.new("1", (128,64))
add_to_image = ImageDraw.Draw(output)
### I have the exterior temp and altitude I want to display. Each has an assigned zone for the icon (FontAwesome) and the data
# temp_ext
temp_zone = [(14,44), (36,64)]
temp_start = (14,44)
temp_icon_zone = [(0,48), (15,64)]
temp_icon_start = (3,48)
add_to_image.text(temp_icon_start, "\uf2c9", font=FA_solid, fill="white")
### every time I have a new reading, I basically draw a black rectangle over what I had and the rewrite the text
add_to_image.rectangle(temp_zone, fill="black", outline = "black")
add_to_image.text(temp_start, str(temp_c), font=text_medium, fill="white")
device.display(output)
這使我能夠只更新我想要的屏幕部分,其余部分保持原樣,最重要的是,在重寫信息時不會有半秒鐘的空白屏幕。歡迎提出優(yōu)化建議!
我仍然需要查看內(nèi)存使用情況,當(dāng)不同區(qū)域同時更新時感覺有點緩慢。但它有效!
添加回答
舉報