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提示词让大模型根据主题生成视频脚本json,然后让Hyperframe渲染出mp4视频?
有哪些字体使用等宽编程代码展示?
如果让演唱会歌迷的上万手机屏幕和闪光灯一起被现场中控控制闪烁?
Midjourney为啥进军医疗领域了?
python如何跟踪足球比赛指定球员全场运动标注打聚光灯合成
如何将linux服务器的文件目录映射到windows电脑磁盘?
Docling 与 MarkItDown 两个库有啥不同?
豆包收费后国产其他ai软件也会跟进收费吗?
JPEG 与 HEIF图片格式区别?
centos7版本太旧无法安装python3.11,如何在docker中运行python3.11?


