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

相关推荐
Warson_L10 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅10 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅10 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L10 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅10 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L11 小时前
python的类&继承
python
Warson_L11 小时前
类型标注/type annotation
python
ThreeS13 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵15 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏