Python PIL ImageOps.fit()方法

Python PIL ImageOps.fit()方法

PIL是Python成像库,它为Python解释器提供了图像编辑功能。ImageOps模块包含一些 “现成的 “图像处理操作。这个模块在某种程度上是实验性的,大多数操作只对L和RGB图像起作用。

ImageOps.fit()方法返回图像的大小和裁剪的版本,裁剪成所要求的长宽比和尺寸。

语法 :PIL.ImageOps.fit(image, size, method=0, bleed=0.0, centering=(0.5, 0.5))

参数 :
image – 大小和裁剪的图像。
size – 要求的输出尺寸,以像素为单位,以(宽度,高度)元组的形式给出。
method – 使用何种重采样方法。默认是PIL.Image.NEAREST。
bleed –从所有四个边缘移除图像外侧的边界。
居中 – 控制裁剪位置。

  • 使用(0.5,0.5)进行中心裁剪(例如,如果裁剪宽度,则从左侧取50%,因此从右侧取50%)。
  • (0.0, 0.0)将从左上角开始裁剪(也就是说,如果裁剪宽度,就从右边拿走所有的裁剪,如果裁剪高度,就从底部拿走所有的)。
  • (1.0, 0.0)将从左下角开始裁剪,等等(即如果裁剪宽度,则从左边全部裁剪,如果裁剪高度,则不从上面裁剪,因此全部从下面裁剪)。

返回:一个图像。

使用的图片:
Python PIL ImageOps.fit()方法

# Importing Image and ImageOps module from PIL package
from PIL import Image, ImageOps
  
# creating a image1 object
im1 = Image.open(r"C:\Users\System-Pc\Desktop\circleimage.PNG")
  
# applying fit method
# Setting width = 100 and height = 100
im2 = ImageOps.fit(im1, (100, 100), method = 0,
                   bleed = 0.0, centering =(0.5, 0.5))
  
im2.show()

输出:
Python PIL ImageOps.fit()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程