如何在你的Raspberry Pi相框中僅顯示豎幅照片
這篇文章可能只針對(duì)一小部分讀者,但這就是像這樣的博客的樂趣所在:你可以深入探索各種極客話題。
已在搭載 Bookworm Wayland 的 Raspberry Pi 5(2024年11月)上測(cè)試。
Pi3D PictureFrame允許在相框?yàn)闄M屏方向時(shí)并排顯示兩張豎幅模式的照片。
因此,我想有一個(gè)簡(jiǎn)單的功能會(huì)很不錯(cuò),這個(gè)功能可以自動(dòng)將你添加到圖片文件夾中的圖像按豎幅、橫幅和正方形分類,這樣你就可以選擇只顯示其中一種。
同樣,當(dāng)你將你的數(shù)碼相框以豎屏方向安裝時(shí),只顯示豎幅照片會(huì)更好。以豎屏模式顯示的橫幅照片看起來會(huì)非常小。
因此,這里有一個(gè)Python腳本,它可以對(duì)你放入圖片文件夾的照片進(jìn)行分類,以及一個(gè)在啟動(dòng)時(shí)運(yùn)行以保持腳本運(yùn)行的服務(wù)。
然后,你可以使用 Home Assistant 或通過MQTT或HTTP命令選擇只顯示豎幅目錄。如果你的相框可以旋轉(zhuǎn)為豎屏或橫屏方向,那就太棒了。
用于分類的Python腳本
使用像Sublime這樣的編輯器或以下命令創(chuàng)建一個(gè)腳本:
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()
保存并關(guān)閉。
使文件可執(zhí)行:
chmod +x /home/pi/sort.py
安裝watchdog
Python有一個(gè)很棒的功能,當(dāng)在目錄中檢測(cè)到新文件時(shí),它會(huì)觸發(fā)一個(gè)命令。
但要在腳本中使用它,你首先需要安裝它:
source venv_picframe/bin/activatepip install pillow watchdog
現(xiàn)在,你可以通過輸入以下命令來測(cè)試腳本是否工作:
python sort.py
創(chuàng)建系統(tǒng)服務(wù)
為了讓腳本始終在后臺(tái)運(yùn)行,為腳本創(chuàng)建一個(gè)系統(tǒng)服務(wù)文件:
sudo nano /etc/systemd/system/sort_pictures.service
將以下內(nèi)容粘貼到文件中:
[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
保存并關(guān)閉。
然后逐行輸入以下命令以激活服務(wù):
sudo systemctl daemon-reloadsudo systemctl enable sort_pictures.servicesudo systemctl start sort_pictures.service
使用以下命令檢查服務(wù)的狀態(tài),以確認(rèn)它正在運(yùn)行:
sudo systemctl status sort_pictures.service
現(xiàn)在,將一些圖像放入你的圖片文件夾中。
腳本應(yīng)該根據(jù)它們的尺寸將它們移動(dòng)到指定的子目錄中。
豎幅選項(xiàng)
現(xiàn)在你可以嘗試兩件事。
一是如果可能的話,更改相框的掛載方式為豎屏,并更改Pi3D PictureFrame中的設(shè)置。按照“如何在Raspberry Pi數(shù)碼相框中使用豎屏方向”中的說明進(jìn)行操作。
如何將你的樹莓派數(shù)字相框設(shè)置為縱向使用:https://www.thedigitalpictureframe.com/raspberry-pi-digital-picture-frame-portrait-orientation/如果你不能這樣做,你可以通過更改configuration.yaml中的這一行來嘗試豎幅對(duì):
portrait_pairs: True
要僅顯示豎向(縱向)的照片,您可以在configuration.yaml中更改默認(rèn)的“Pictures”目錄,或者如果您已安裝Home Assistant,則可以通過它來設(shè)置目錄。
或者,您也可以暫時(shí)從主“Pictures”目錄中移除“Landscape”(橫向)和“Square”(方形)目錄。祝您使用愉快!
-
python
+關(guān)注
關(guān)注
56文章
4820瀏覽量
85492 -
Raspberry Pi
+關(guān)注
關(guān)注
2文章
613瀏覽量
22547
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
如何在Raspberry Pi上安裝TensorFlow

如何在Raspbian上設(shè)置沒有顯示器和鍵盤的Raspberry Pi

如何制作Raspberry Pi樹莓派的SD卡
極致小巧的樹莓派新成員,僅 5 美金的 Raspberry Pi Zero 登場(chǎng)
【Raspberry Pi 3試用體驗(yàn)】試用進(jìn)程大匯總(2016.6.21已更新)
Raspberry Pi 3和3 b +上的Android Pie 9.0
使用raspberry pi Pico的原因
raspberry_pi各版本差別
工業(yè)環(huán)境中的Raspberry PI和Arduino
raspberry pi官網(wǎng)
如何在Raspberry Pi 3上安裝OpenCV4庫

如何在Raspberry Pi Pico中使用OLED顯示器

使用Raspberry Pi 3自制智能相框和日歷—第二部分

如何在Raspberry Pi零2W上阻止帶有Pi孔的廣告

為Raspberry Pi添加15美元的顯示器

評(píng)論