因为自己朋友做电商,让我给他做了一个自动批量选择图片裁剪成800*800(正方形,不足800的以实际尺寸为准,超过的只裁剪中心点开始的800*800)尺寸的工具
用起来很快,hhhh
现在将成品exe和py代码分享给大家
能够帮到大家我很高兴
图片展示:
1. 选择图片(可以批量选中)

2.选择输入文件夹

3.生成预览(会自动生成文件夹)

exe成品下载:https://ljk.lanzouu.com/iYols2qxysnc 密码:52pj
[Python] 纯文本查看 复制代码 from PIL import Image
import tkinter as tk
from tkinter import filedialog
import os
from datetime import datetime
def crop_images():
# 创建一个隐藏的 Tkinter 窗口
root = tk.Tk()
root.withdraw()
# 弹出文件选择对话框,允许选择多个图片文件
file_paths = filedialog.askopenfilenames(filetypes=[("Image files", "*.png;*.jpg;*.jpeg")])
if file_paths:
# 弹出文件夹选择对话框,让用户选择保存裁剪后图片的根文件夹
output_root_folder = filedialog.askdirectory()
if output_root_folder:
# 获取当前时间并格式化
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
# 生成新文件夹名称
new_folder_name = f"{current_time}ki裁剪800"
# 拼接新文件夹的完整路径
new_folder_path = os.path.join(output_root_folder, new_folder_name)
# 创建新文件夹
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
for file_path in file_paths:
try:
# 打开图片
image = Image.open(file_path)
width, height = image.size
# 确定裁剪尺寸
if width < 800 or height < 800:
crop_size = min(width, height)
else:
crop_size = 800
# 计算裁剪的坐标
left = (width - crop_size) // 2
top = (height - crop_size) // 2
right = left + crop_size
bottom = top + crop_size
# 裁剪图片
cropped_image = image.crop((left, top, right, bottom))
# 生成保存路径
file_name = file_path.split("/")[-1]
output_name = file_name.rsplit('.', 1)[0] + '_cropped.' + file_name.rsplit('.', 1)[1]
output_path = os.path.join(new_folder_path, output_name)
# 保存裁剪后的图片
cropped_image.save(output_path)
print(f"裁剪并保存图片: {output_path}")
except Exception as e:
print(f"处理图片 {file_path} 时出错: {e}")
if __name__ == "__main__":
crop_images() |