如何在你的Raspberry Pi相框中僅顯示豎幅照片
這篇文章可能只針對一小部分讀者,但這就是像這樣的博客的樂趣所在:你可以深入探索各種極客話題。
已在搭載 Bookworm Wayland 的 Raspberry Pi 5(2024年11月)上測試。
Pi3D PictureFrame允許在相框為橫屏方向時并排顯示兩張豎幅模式的照片。
因此,我想有一個簡單的功能會很不錯,這個功能可以自動將你添加到圖片文件夾中的圖像按豎幅、橫幅和正方形分類,這樣你就可以選擇只顯示其中一種。
同樣,當你將你的數碼相框以豎屏方向安裝時,只顯示豎幅照片會更好。以豎屏模式顯示的橫幅照片看起來會非常小。
因此,這里有一個Python腳本,它可以對你放入圖片文件夾的照片進行分類,以及一個在啟動時運行以保持腳本運行的服務。
然后,你可以使用 Home Assistant 或通過MQTT或HTTP命令選擇只顯示豎幅目錄。如果你的相框可以旋轉為豎屏或橫屏方向,那就太棒了。
用于分類的Python腳本
使用像Sublime這樣的編輯器或以下命令創建一個腳本:
sudo nano sort.py
然后將以下文本粘貼到文件中:
import osimport shutilimport timefrom PIL import Image, UnidentifiedImageErrorfrom watchdog.observers import Observerfrom watchdog.events import FileSystemEventHandler
# Define pathspictures_folder = "/path/to/Pictures"portrait_folder = os.path.join(pictures_folder, "Portrait Orientation")landscape_folder = os.path.join(pictures_folder, "Landscape Orientation")square_folder = os.path.join(pictures_folder, "Square Images")
# Create folders if they don't existos.makedirs(portrait_folder, exist_ok=True)os.makedirs(landscape_folder, exist_ok=True)os.makedirs(square_folder, exist_ok=True)
# Set to track skipped filesskipped_files = set()
def is_file_complete(file_path, wait_time=1): """ Check if a file is fully copied by comparing its size multiple times with a delay. """ for _ in range(3): # Check 3 times to ensure completion initial_size = os.path.getsize(file_path) time.sleep(wait_time) final_size = os.path.getsize(file_path) if initial_size == final_size: return True return False
def classify_image(file_path): try: if is_file_complete(file_path): with Image.open(file_path) as img: width, height = img.size if width > height: destination = landscape_folder elif height > width: destination = portrait_folder else: destination = square_folder shutil.move(file_path, destination) print(f"Moved {file_path} to {destination}") # Remove from skipped files if it was previously skipped if file_path in skipped_files: skipped_files.remove(file_path) else: print(f"File {file_path} is still being copied. Adding to skipped list.") skipped_files.add(file_path) except UnidentifiedImageError: print(f"Cannot identify image file {file_path}. Adding to skipped list.") skipped_files.add(file_path) except Exception as e: print(f"Error processing {file_path}: {e}")
def classify_images_in_folder(): for filename in os.listdir(pictures_folder): file_path = os.path.join(pictures_folder, filename) if os.path.isfile(file_path) and filename.lower().endswith(".jpg"): classify_image(file_path)
class ImageHandler(FileSystemEventHandler): def on_created(self, event): if event.is_directory: return if event.src_path.lower().endswith(".jpg"): classify_image(event.src_path)
def on_moved(self, event): if not event.is_directory and event.dest_path.lower().endswith(".jpg"): classify_image(event.dest_path)
def on_modified(self, event): if not event.is_directory and event.src_path.lower().endswith(".jpg"): classify_image(event.src_path)
def retry_skipped_files(): """ Retry classifying files that were previously skipped due to incomplete copying or unidentifiable errors. """ for file_path in list(skipped_files): # Iterate over a copy of the set if os.path.exists(file_path): print(f"Retrying {file_path}") classify_image(file_path)
if __name__ == "__main__": # Initial classification classify_images_in_folder()
# Set up the observer observer = Observer() event_handler = ImageHandler() observer.schedule(event_handler, path=pictures_folder, recursive=False) observer.start()
try: while True: retry_skipped_files() # Periodically retry skipped files time.sleep(5) # Adjust this sleep time as needed except KeyboardInterrupt: observer.stop() observer.join()
保存并關閉。
使文件可執行:
chmod +x /home/pi/sort.py
安裝watchdog
Python有一個很棒的功能,當在目錄中檢測到新文件時,它會觸發一個命令。
但要在腳本中使用它,你首先需要安裝它:
source venv_picframe/bin/activatepip install pillow watchdog
現在,你可以通過輸入以下命令來測試腳本是否工作:
python sort.py
創建系統服務
為了讓腳本始終在后臺運行,為腳本創建一個系統服務文件:
sudo nano /etc/systemd/system/sort_pictures.service
將以下內容粘貼到文件中:
[Unit]Description=Sort Pictures ServiceAfter=network.target
[Service]ExecStart=/home/pi/venv_picframe/bin/python /home/pi/sort.pyWorkingDirectory=/home/piRestart=alwaysUser=pi
[Install]WantedBy=multi-user.target
保存并關閉。
然后逐行輸入以下命令以激活服務:
sudo systemctl daemon-reloadsudo systemctl enable sort_pictures.servicesudo systemctl start sort_pictures.service
使用以下命令檢查服務的狀態,以確認它正在運行:
sudo systemctl status sort_pictures.service
現在,將一些圖像放入你的圖片文件夾中。
腳本應該根據它們的尺寸將它們移動到指定的子目錄中。
豎幅選項
現在你可以嘗試兩件事。
一是如果可能的話,更改相框的掛載方式為豎屏,并更改Pi3D PictureFrame中的設置。按照“如何在Raspberry Pi數碼相框中使用豎屏方向”中的說明進行操作。
如何將你的樹莓派數字相框設置為縱向使用:https://www.thedigitalpictureframe.com/raspberry-pi-digital-picture-frame-portrait-orientation/如果你不能這樣做,你可以通過更改configuration.yaml中的這一行來嘗試豎幅對:
portrait_pairs: True
要僅顯示豎向(縱向)的照片,您可以在configuration.yaml中更改默認的“Pictures”目錄,或者如果您已安裝Home Assistant,則可以通過它來設置目錄。
或者,您也可以暫時從主“Pictures”目錄中移除“Landscape”(橫向)和“Square”(方形)目錄。祝您使用愉快!
-
python
+關注
關注
58文章
4880瀏覽量
90203 -
Raspberry Pi
+關注
關注
2文章
622瀏覽量
24068
發布評論請先 登錄
如何在Raspberry Pi上安裝TensorFlow
如何在Raspbian上設置沒有顯示器和鍵盤的Raspberry Pi
如何制作Raspberry Pi樹莓派的SD卡
極致小巧的樹莓派新成員,僅 5 美金的 Raspberry Pi Zero 登場
【Raspberry Pi 3試用體驗】試用進程大匯總(2016.6.21已更新)
Raspberry Pi 3和3 b +上的Android Pie 9.0
工業環境中的Raspberry PI和Arduino
raspberry pi官網
如何在Raspberry Pi 3上安裝OpenCV4庫
如何在Raspberry Pi Pico中使用OLED顯示器
使用Raspberry Pi 3自制智能相框和日歷—第二部分
使用Raspberry Pi 3自制智能相框和日歷—第一部分
如何在Raspberry Pi零2W上阻止帶有Pi孔的廣告
解鎖垂直美學!如何在你的Raspberry Pi相框中僅顯示豎版照片!
評論