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

相关推荐
专注仿真38 分钟前
仿真智能体训练:强化学习中的生存、任务、杀伤、能耗与规则惩罚算法详解与实战
python·算法·afsim智能体·仿真训练·afsim智能体训练
Uio2411 小时前
Java集合:ArrayList与LinkedList核心对比(实战+源码要点)
笔记
cpolar技术支持2 小时前
datetime.now() 和 datetime.now(timezone.utc) 混进 SQLite:零报错,读出来差 8 小时
python·sqlite·时间戳·datetime·时区
旖旎夜光2 小时前
面试题 01.02: 判定是否互为字符重排 (哈希表) —— 题解
c++·笔记·学习·算法·哈希表
维爱智VR认知学能2 小时前
Python SQLite本地训练记录表迁移失败回滚与事务验证
数据库·python·单元测试·sqlite·软件工程
用户298698530143 小时前
使用 Python 替换 Word 文档文字:从全文到指定范围
python·api
Thanks_ks3 小时前
手写一个 LRU 缓存:哈希表与双向链表的组合设计
python·哈希表·lru·双向链表·ordereddict·命中率·哨兵节点
AI创界者15 小时前
Python 进阶实战(七十一章):研发效能度量、自动化质量门禁与工程规范落地实战
人工智能·python·aigc
yingjie11015 小时前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python
tryCbest16 小时前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python