python如何批量将图片不裁剪不变形填充式固定长宽比编辑图片?
网友回复
代码
import tkinter as tk from tkinter import filedialog from PIL import Image import os def resize_and_fill_image(input_path, output_path, target_width, target_height): # 打开原始图片 image = Image.open(input_path) width, height = image.size # 计算新的尺寸,保持图片的宽高比 ratio = min(target_width / width, target_height / height) new_width = int(width * ratio) new_height = int(height * ratio) # 调整图片大小 resized_image = image.resize((new_width, new_height), Image.LANCZOS) # 创建一个新的空白图片,使用白色填充 result_image = Image.new("RGB", (target_width, target_height), (255, 255, 255)) # 计算粘贴的位置,使图片居中 x_offset = (target_width - new_width) // 2 y_offset = (target_height - new_height) // 2 # 将调整后的图片粘贴到新的空白图片上 result_image.paste(resized_image, (x_offset, y_offset)) # 保存处理后的图片 result_image.save(output_path) def batch_process_images(input_folder, output_folder, target_width, target_height): # 确保输出文件夹存在 if not os.path.exists(output_folder): os.makedirs(output_folder) # 遍历输入文件夹中的所有图片文件 for filename in os.listdir(input_folder): if filename.lower...
点击查看剩余70%
人形机器人的运动能否有端侧ai全面接管?
有没有抓取抖音头条等自媒体平台指定主题的评论的python开源程序?
css如何实现多个代码块向下滚动右上角复制按钮sticky粘性在顶部效果?
python+Quill如何实现多人实时文档编辑html?
什么是ai的6A工作流规则?
&、nohup、screen、tmux在linux中后台执行的区别?
python如何将调用ai大模型生成的文件修改行操作指令修改原文件后保存?
python如何将2d平面线图转换成数字2d线稿图?
acejs代码编辑器如何调用openai api实现选择代码修改与代码自动补全?
ace.js如何获取选择文本的开始和结束行数?