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%
为什么主流大模型架构都在用MoE,而不是传统的Dense?
js如何将图片转换成拼豆图案风格?
ai能接管电脑手机写作剪辑视频自主运营自媒体账号为用户赚钱吗?
python+qwen的api如何实现类似skills的技能创建与自主调用?
python+openai兼容api如何实现自主调用浏览器搜索登录发布信息?
安卓手机投屏电视视频播放结束如何自动播放下一集?
PaddleOCR-VL-1.5与deepseek ocr2谁更好?
电商系统中优惠券规则引擎与组合优惠如何设计避免在代码中重复使用ifelse?
Grok Imagine Video这个ai能根据用户文本指令编辑视频吗?
Openai的Prism到底是啥?


