• input输入路径,读取图片尺寸,移动手机截图“满屏”相同尺寸图片到别的文件夹


    安装Pillow库是一个图像库,获取图片的尺寸,并移动相同尺寸图片到文件夹

    input默认读取 /storage/emulated/0/Pictures/Screenshots/Screenshot_20231001_132228.jpg 尺寸大小
    输入错误就直接读取这个路径图片尺寸,
    将和它相同尺寸的图片移动到 /storage/emulated/0/Pictures/图片要删除/
    没有这个文件夹就新建
    
    • 1
    • 2
    • 3
    • 4

    可以使用Python编程语言来实现这个任务。首先,需要安装Pillow库,它是一个处理图像的库。可以使用以下命令来安装它:

    pip install pillow
    
    • 1

    然后,使用以下代码来获取指定图片的尺寸:

    from PIL import Image
    
    image_path = '/storage/emulated/0/Pictures/Screenshots/Screenshot_20231001_132228.jpg'
    
    try:
        image = Image.open(image_path)
        width, height = image.size
        print("图片尺寸大小:{}x{}".format(width, height))
    except FileNotFoundError:
        print("找不到指定的图片文件。")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    接下来,可以使用shutil库来移动相同尺寸的图片到目标文件夹。请确保已经安装了shutil库。使用以下代码来移动相同尺寸的图片:

    import os
    import shutil
    
    source_folder = '/storage/emulated/0/Pictures/'
    target_folder = '/storage/emulated/0/Pictures/图片要删除/'
    
    # 创建目标文件夹(如果不存在)
    if not os.path.exists(target_folder):
        os.makedirs(target_folder)
    
    for filename in os.listdir(source_folder):
        file_path = os.path.join(source_folder, filename)
        if os.path.isfile(file_path):
            try:
                image = Image.open(file_path)
                if image.size == (width, height):
                    shutil.move(file_path, target_folder)
            except:
                pass
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    以上代码将遍历指定文件夹中的所有文件,将尺寸与指定图片相同的文件移动到目标文件夹中。

    请注意,需要根据实际情况修改文件路径,并确保路径是正确的。

  • 相关阅读:
    [计算机网络] 虚拟局域网
    1、SpringCloud大型企业分布式微服务云架构之Markdown 教程
    【Leetcode HOT100】和为 K 的子数组 c++
    【LeetCode】【剑指offer】【二叉树中和为某一值的路径】
    深入理解 python 虚拟机:原来虚拟机是这么实现闭包的
    【day5】数组
    1072 Gas Station
    使用大语言模型 LLM 做文本分析
    安全渗透测试基础知识之网络基础知识
    带你吃透Servlet技术
  • 原文地址:https://blog.csdn.net/weixin_73675558/article/details/133463662