第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 PIL/Python 將元素添加到 OLED 顯示而不擦除剩余元素

通過 PIL/Python 將元素添加到 OLED 顯示而不擦除剩余元素

慕娘9325324 2023-10-05 16:27:05
我有一個 SH1106 顯示器連接到我的 Raspberry Pi,我使用 luma.oled 控制它。我可以用不同的字體顯示各種內(nèi)容,這很棒。但是,我不知道如何在不刷新整個顯示的情況下向當(dāng)前顯示的內(nèi)容添加內(nèi)容。我的代碼是這樣的:from os import systemimport serialfrom time import sleepfrom luma.core.interface.serial import i2cfrom luma.core.render import canvasfrom luma.oled.device import sh1106from PIL import ImageFont# config displaydevice = sh1106(i2c(port=1, address=0x3C), rotate=0)device.clear()FA_solid = ImageFont.truetype('/home/pi/Desktop/tests/fa-solid-900.ttf', 16)FA_regular = ImageFont.truetype('/home/pi/Desktop/tests/fa-regular-400.ttf', 16)text_large = ImageFont.truetype('/home/pi/Desktop/tests/coolvetica condensed rg.ttf', 48)text_small = ImageFont.truetype('/home/pi/Desktop/tests/coolvetica condensed rg.ttf', 16)# display thingsdef show_icon(code):    with canvas(device) as draw:        draw.text((112, 0), text=code, font=FA_solid, fill="white")        def large_text(content, paddingleft =0, paddingtop =0):    with canvas(device) as draw:        draw.text((0, 0), text=content, font=text_large, fill="white")        def small_text(content, paddingleft =0, paddingtop =0):    with canvas(device) as draw:        draw.text((0, 0), text=content, font=text_small, fill="white")show_icon("\uf124")sleep(2)large_text("Hi ;)")sleep(10)device.clear()這會在右上角顯示一個來自 fontawesome 的圖標(biāo),然后清除屏幕并顯示 Hi。如何更改它以顯示圖標(biāo) + hi?理想情況下,我會在屏幕上有“區(qū)域”,我可以在其中更改圖標(biāo)區(qū)域,同時保持文本顯示,反之亦然。謝謝!
查看完整描述

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)

這是原始顯示:

https://img1.sycdn.imooc.com//651e739a0001b31001280061.jpg

更新 zone2 后再次出現(xiàn):

https://img1.sycdn.imooc.com//651e73a7000126b201280063.jpg


查看完整回答
反對 回復(fù) 2023-10-05
?
瀟瀟雨雨

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ū)域同時更新時感覺有點緩慢。但它有效!


查看完整回答
反對 回復(fù) 2023-10-05
  • 2 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號