Python 图像处理库PIL ImageOps笔记

复制代码
# 返回一个指定大小的裁剪过的图像。该图像被裁剪到指定的宽高比和尺寸。
# 变量size是要求的输出尺寸,以像素为单位,是一个(宽,高)元组
# bleed:允许用户去掉图像的边界(图像四个边界)。这个值是一个百分比数(0.01表示百分之一)。默认值是0(没有边界),最高0.5
# centering: 用于控制裁剪位置。
#     (0.5,0.5) 是裁剪中心(如果裁剪宽度,裁掉左侧50%(右侧50%),顶/底一样)。
#     (0.0,0.0) 将从左上角开始裁剪(如果裁剪宽度,将从右边裁剪掉所要裁剪的部分;如果裁剪高度,将从底部裁剪掉所要裁剪的部分)。
#     (1.0,0.0) 将从左下角开始裁剪(如果裁剪宽度,将从左边裁剪掉所要裁剪的部分;如果裁剪高度,将从底部裁剪掉所要裁剪的部分)




def fit(image, size, method=Image.Resampling.BICUBIC, bleed=0.0, centering=(0.5, 0.5)):
python 复制代码
from PIL import Image, ImageOps


# 1280*720
im02 = Image.open("./sdout/2.png")

# 上左
im0 = ImageOps.fit(im02, (1000,500), Image.BICUBIC, 0.0, (0.0,0.0))
im0.save("./im0.png")
python 复制代码
from PIL import Image, ImageOps


# 1280*720
im02 = Image.open("./sdout/2.png")

# 下 右
im0 = ImageOps.fit(im02, (1000,500), Image.BICUBIC, 0.0, (1.0,1.0))
im0.save("./im0.png")
python 复制代码
from PIL import Image, ImageOps


# 1280*720
im02 = Image.open("./sdout/2.png")

# 上右
im3 = ImageOps.fit(im02, (1000,500), Image.BICUBIC, 0.0, (0.0,1.0))
im2.save("./im2.png")
python 复制代码
from PIL import Image, ImageOps


# 1280*720
im02 = Image.open("./sdout/2.png")

# 下左
im3 = ImageOps.fit(im02, (1000,500), Image.BICUBIC, 0.0, (1.0,0.0))
im2.save("./im2.png")

参考:

Python图像处理库PIL的ImageOps模块介绍 -----> 一些基本的图像操作_修炼打怪的小乌龟的博客-CSDN博客

PythonInformer - Image resizing recipes in Pillow

相关推荐
AI探索者6 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者6 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh8 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅8 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽9 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时12 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿15 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python