安装Pillow库是一个图像库,获取图片的尺寸,并移动相同尺寸图片到文件夹
input默认读取 /storage/emulated/0/Pictures/Screenshots/Screenshot_20231001_132228.jpg 尺寸大小
输入错误就直接读取这个路径图片尺寸,
将和它相同尺寸的图片移动到 /storage/emulated/0/Pictures/图片要删除/
没有这个文件夹就新建
可以使用Python编程语言来实现这个任务。首先,需要安装Pillow库,它是一个处理图像的库。可以使用以下命令来安装它:
pip install pillow
然后,使用以下代码来获取指定图片的尺寸:
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("找不到指定的图片文件。")
接下来,可以使用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
以上代码将遍历指定文件夹中的所有文件,将尺寸与指定图片相同的文件移动到目标文件夹中。
请注意,需要根据实际情况修改文件路径,并确保路径是正确的。