0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

解锁垂直美学!如何在你的Raspberry Pi相框中仅显示竖版照片!

上海晶珩电子科技有限公司 ? 2025-03-25 09:33 ? 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

如何在你的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
    +关注

    关注

    56

    文章

    4828

    浏览量

    87096
  • Raspberry Pi
    +关注

    关注

    2

    文章

    620

    浏览量

    23178
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    何在Raspberry Pi上安装TensorFlow

     在本教程,我们将学习如何在 Raspberry Pi 上安装 TensorFlow,并将展示一些在预训练神经网络上进行简单图像分类的示例。
    发表于 09-01 16:35 ?2557次阅读
    如<b class='flag-5'>何在</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>上安装TensorFlow

    何在Raspbian上设置没有显示器和键盘的Raspberry Pi

    在本教程,我们将了解如何在新安装的 Raspbian 上设置没有显示器和键盘的 Raspberry Pi
    发表于 09-22 16:31 ?2131次阅读
    如<b class='flag-5'>何在</b>Raspbian上设置没有<b class='flag-5'>显示</b>器和键盘的<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>

    如何制作Raspberry Pi树莓派的SD卡

    :raspberryi) 现在你会看到提示符:pi@raspberry ~ $j) 想要进入图形界面输入:startxk) 你会发现你已经进入了一个即熟悉又不同的桌面环境l) 好了,发现你已经制作好树莓派的SD卡了。
    发表于 06-30 23:53

    极致小巧的树莓派新成员, 5 美金的 Raspberry Pi Zero 登场

    到 25 美金,不过他们仍希望能更进一步压低价格,于是在稍早树莓派家族的新成员 Raspberry Pi Zero 亮相,将价格定在 5 美金。新闻来源: Raspberry
    发表于 11-26 22:46

    Raspberry Pi 3试用体验】试用进程大汇总(2016.6.21已更新)

    /jishu_583079_1_1.html5、【Raspberry Pi 3试用体验】+ 中文显示及输入+百度云传输(4.22)https://bbs.elecfans.com/jishu_583519_1_1.html6
    发表于 04-14 21:32

    Raspberry Pi 3和3 b +上的Android Pie 9.0

    Android Pie 9.0。在这个视频,我将向您展示如何在最新的Android版本的raspberry pi 3上安装android 9 Pie。 我在此视频中使用的设备:
    发表于 09-29 14:28

    使用raspberry pi Pico的原因

    使用raspberry pi Pico的原因在硬件产品(单片机)的开发我们往往需要借助一些额外的仪器/设备进行产品的辅助测试, 假设我们需要一个IO+ADC类型辅助设备, 以往的做法是 原理图
    发表于 02-07 09:16

    raspberry_pi各版本差别

    raspberry pi 各版本差别,对比Raspberry Pi Model B+、Raspberry
    发表于 01-06 11:12 ?0次下载

    工业环境Raspberry PI和Arduino

    Raspberry PI和Arduino板是快速电子成型和家庭DIY应用中非常有名的设备,不过他们在工业环境的功能性和灵活性在很大程度上还有待评估。Raspberry
    发表于 06-23 11:32 ?4693次阅读

    raspberry pi官网

    Raspberry Pi 宣布推出新的镜像实用程序 Raspberry Pi Imager,以提供一种更简单的方法,将操作系统轻松镜像到 microSD 上。
    的头像 发表于 03-07 10:16 ?6563次阅读

    何在Raspberry Pi 3上安装OpenCV4库

    今天我们将学习如何在 Raspberry Pi 3 上安装 OpenCV4 库,以便我们可以将其用于计算机视觉应用程序。这将允许 OpenCV 在像 Pi 这样的便携式设备上运行,从而
    的头像 发表于 09-08 16:09 ?1964次阅读
    如<b class='flag-5'>何在</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b> 3上安装OpenCV4库

    何在Raspberry Pi Pico中使用OLED显示

    电子发烧友网站提供《如何在Raspberry Pi Pico中使用OLED显示器.zip》资料免费下载
    发表于 10-18 09:15 ?4次下载
    如<b class='flag-5'>何在</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b> Pico中使用OLED<b class='flag-5'>显示</b>器

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

    本教程的第二部分将带您完成构建智能相框和日历的框架、连接PIR运动传感器和控制相框幻灯片显示向日历显示的过渡这些步骤。该项目将继续使用本教程的使用R
    的头像 发表于 02-24 17:51 ?1834次阅读
    使用<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b> 3自制智能<b class='flag-5'>相框</b>和日历—第二部分

    何在Raspberry Pi零2W上阻止带有Pi孔的广告

    电子发烧友网站提供《如何在Raspberry Pi零2W上阻止带有Pi孔的广告.zip》资料免费下载
    发表于 06-14 10:38 ?0次下载
    如<b class='flag-5'>何在</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>零2W上阻止带有<b class='flag-5'>Pi</b>孔的广告

    Raspberry Pi添加15美元的显示

    电子发烧友网站提供《为Raspberry Pi添加15美元的显示器.zip》资料免费下载
    发表于 06-20 11:04 ?0次下载
    为<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>添加15美元的<b class='flag-5'>显示</b>器