Try   HackMD

MicroPython GIF 動態圖

tags: 致敬 | MicroPython | ESP32

PaoyungJun 29, 2022

ℳ𝒾𝒸𝓇ℴ𝒫𝓎𝓉𝒽ℴ𝓃 隨手記

相關文章 👉 MicroPython 真動態圖

前言

這是一篇致敬文。目的是用 MicroPython 以更為簡捷的方式達成與 Arduino IDE 相同的程式效果。

先看一段影片:(前段為静態圖的說明,動態的部份請從 03:55 開始觀賞)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

動態圖片在顯示上比單一圖片來得活潑且具有較佳的互動感,在各類顯示螢幕上已是必備的元素了。而 GIF 格式的圖檔裡包含了多張相關且連續的圖片,依續播放則能達成動態的效果,其在網頁上已被大量的運用,因此是取得容易的素材之一,若能成為小型裝置上的動態圖片來源,即可先看到呈現的效果,且能降低製作和取材的難度。但從以上的影片中可以得知要將 GIF 在 Arduino IDE 架構中置於小型如 SSD1306 OLED 的設備上來顯示,仍需經過以下幾個步驟:

  1. 從網路下載 GIF。
  2. 利用網路工具將 GIF 分成多個 frames 圖檔。
  3. 利用圖片軟體批次改變大小,並存成 bitmap 格式圖檔。
  4. 使用影片作者所撰寫的工具製作出資料格式片段及程式片段。
  5. 將上述的資料片段及程式片段貼至主程式中。

換人做做看

從上述的程序中可以發現需要切換使用不同的工具或軟體來達成各個階段性目標,才能再往再下一步驟進行。若換成 MicroPython 的生態又該怎麼做呢? 如同之前運用字庫的處理方式,會先以 Python 處理前段,後續再由 MicroPython 接手。在 Python 中有個強大的影像處理函式庫 PIL,只要調用它就可以把上述的步驟 2~4 簡化成單一步驟,且個人認為轉出來的圖檔細緻度頗佳。簡化後如下:

  1. 從網路下載 GIF。
  2. 使用 Python 分 frames、resize、轉單色並存為 list 供後續載入調用。
  3. 將步驟 2. 的 list 上傳並載入即可秀出 GIF 效果。

無憂

Python 這個方便的影像處理函式庫叫做 PIL,即 Python Imaging Library,但它不再支援新版的 Python,而 Pillow 是 PIL 的分支且持續更新中,它已完全的取代了 PIL,且在程式中仍以 PIL 的名稱來調用,所以可以延用原本的方法和參數。若您不曾安裝 Pillow 函式庫,操作前請先使用以下指令安裝。

pip install pillow

參考: Pillow 的 github

步驟 2 的程式如下:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
使用 Python 執行分 frames、resize、轉單色及存 list,產生的檔案需上傳至 MicroPython

調用

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
MicroPython 以 SSD1306 顯示動態圖 (SSD1306 需先做好設定)

SSD1306設定請參考: my_util.py 說明

展示

若您已觀賞完以 Arduino IDE 來完成 GIF 的影片並瞭解其過程,會發現在 Python 與 MicroPython 的聯手配搭下,整個過程變得相對簡單且快速許多,而本篇提供的程式可直接取用執行即可生成 frames 檔,也可做為參考並修改成更適合自己的版本。以下即為程式執行的輸出結果。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Paoyung ChangSat, Jul 2, 2022 3:55 PM

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
別走開,續集來囉! MicroPython 真動態圖

關於作者  

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

我是 𝙋𝙖𝙤𝙮𝙪𝙣𝙜,是 MicroPythonEspruino 的愛好者,一直認為:

「Maker 應該把重心放在應用上,而不該被語言工具限制了創意」

所以想藉由較為簡捷且詳細的解說讓更多人可以學習進而自由發揮,而非淪為只能將程式碼複製貼上的複製人,如果你認同這樣的理念,請一起為此目標努力,如果我的文章內容對你有幫助,請轉發或協助需要的人。

若有專案需求,請利用 連絡我,謝謝!