[Pillow]-Reference-Image Module-Image processing

The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

Examples

Open, rotate, and display an image (using the default viewer)

The following script loads an image, rotates it 45 degrees, and displays it using an external viewer (usually xv on Unix, and the Paint program on Windows).

from PIL import Image
with Image.open("hopper.jpg") as im:
    im.rotate(45).show()

Create thumbnails

The following script creates nice thumbnails of all JPEG images in the current directory preserving aspect ratios with 128x128 max resolution.

from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    with Image.open(infile) as im:
        im.thumbnail(size)
        im.save(file + ".thumbnail", "JPEG")

Functions

PIL.Image.open(fp, mode='r', formats=None)

Parameters

  • fp – A filename (string), pathlib.Path object or a file object. The file object must implement file.read, file.seek, and file.tell methods, and be opened in binary mode.

  • mode – The mode. If given, this argument must be “r”.

  • formats – A list or tuple of formats to attempt to load the file in. This can be used to restrict the set of formats checked. Pass None to try all supported formats. You can print the set of available formats by running python3 -m PIL or using the [PIL.features.pilinfo()]

Returns

  • An Image object.
    -------------------------------------------------------Image processing--------------------------------------------------------------------

Image.alpha_composite(im1, im2)

Image.alpha_composite(im1, im2):在im1对象上的透明层复合im2,返回一个Image对象
im1:Image对象1
im2:Image对象2

from PIL import Image
im1 = Image.open(path1)
im2 = Image.open(path2)
im3 = Image.alpha_composite(im1,im2)

Image.blend(im1, im2, alpha)

Image.blend(im1, im2, alpha):在两个图片对象之间进行插值,返回一个Image对象
im1:Image对象1
im2:Image对象2
alpha:透明图
如果alpha为0.0,则返回第一个图像的副本。如果alpha为1.0,则返回第二个图像的副本,基本的算法如下:

out  =  image1  *  (1.0  -  alpha ) +  image2  *  alpha

Image.eval(image, *args)

Image.eval(image, *args):将函数应用于给定图像的中每一个像素。请注意,该函数对每个可能的像素值都进行一次评估,因此您不能使用随机组件或其他生成器。返回一个Image对象
image:Image对象
args:一个函数对象和该函数的一个取整参数

from PIL import Image
def func(a):
    return a
im1 = Image.open(path1)
img = Image.eval(img1,func,1)

Image.merge(mode, bands)

mage.merge(mode, bands):将一组单波段图像合并成为一个多波段图像。返回一个Image对象
mode:用于输出图像的模式。支持的模式请看下方Pillow支持的模式表
bands:输出图像中每个波段包含一个单波段图像的序列

你可能感兴趣的:([Pillow]-Reference-Image Module-Image processing)