News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

js(0)

jk(0)

tag  

js(0)

date  

no datas

The PIL library in Python

posted on 2023-06-06 10:11     read(670)     comment(0)     like(13)     collect(2)


PIL Basic Syntax

1. Introduction

1. Basic introduction

Pillow is a relatively basic image processing library in Python. It is mainly used for basic image processing, such as cropping images, adjusting image sizes, and image color processing. Compared with Pillow, OpenCV and Scikit-image have richer functions, so they are more complicated to use. They are mainly used in machine vision, image analysis and other fields, such as the well-known "face recognition" application.

2. Features

  1. Many formats are supported

    Pillow supports a wide range of image formats, such as "jpeg", "png", "bmp", "gif", "ppm", "tiff", etc. At the same time, it also supports mutual conversion between image formats. In short, Pillow can handle images of almost any format

  2. Provide rich functions

    Pillow provides a wealth of image processing functions, which can be summarized in two aspects:

    • image archive
    • Image Processing

    Image archiving, including creating thumbnails, generating preview images, image batch processing, etc.; and image processing, including resizing images, cropping images, pixel processing, adding filters, image color processing, etc.

  3. Use with GUI tools

Official document:【https://www.osgeo.cn/pillow/reference/ImageFont.html】

3. Installation

pip install pillow
导包
imoprt PIL

2. Image object

1. Instantiate the object

1.1 Instantiation

Guide package

from PIL import Image

use the open method

im = PIL.Image.open(fp)  # 导入图片
im.show()  # 展示图片

fp: image path

use the open method

im = Image.new(mode,size,color)  # 创建图片
im.show()  # 展示图片

The parameters are described as follows:

  • mode: image mode, string parameters, such as RGB (true color image), L (grayscale image), CMYK (color map printing mode), etc.
  • size: image size, the tuple parameter (width, height) represents the pixel size of the image
  • color: the color of the picture, the default value is 0 means black, the parameter value supports (R, G, B) triplet number format, the hexadecimal value of the color and the English word of the color

1.2 Image Mode

modedescribe
11-bit pixel (value range 0-1), 0 means black, 1 means white, monochrome channel.
L8-bit pixel (value range 0-255), grayscale image, monochrome channel.
P8-bit pixels, using palette mapping to any other mode, monochrome channel.
RGB3 x 8-bit pixels, true color, three color channels, the value range of each channel is 0-255.
RGBA4 x 8-bit pixels, true color + alpha channel, four color channels.
CMYK4 x 8-bit pixels, four color channels, can be adapted to print pictures.
YCbCr3 x 8-bit pixels, color video format, three color channels.
LAB3 x 8-bit pixels, L*a*b color space, three color channels
HSV3 x 8-bit pixels, hue, saturation, value color space, three color channels.
I32-bit signed integer pixels, monochrome channel.
F32-bit floating point pixels, monochrome channel.

2. Object properties

import PIL.Image

im = PIL.Image.open(r"D:\35005\Pictures\Screenshots\微信图片_20220302175157.jpg")
print(im.size)  # 查看图片大小
print(im.readonly)  # 查看是否为只读,1为是,0为否
print(im.format)  # 查看图片的格式
print(im.info)  # 查看图片的相关信息
print(im.mode)  # 查看图片的模式

3. Format conversion

3.1 save method

The save method is used to save the image. When the file format is not specified, it will be stored in the default image format; if the image format is specified, the image will be stored in the specified format

grammar:

im = PIL.Image.open(r"D:\35005\Pictures\Screenshots\微信图片_20220302175157.jpg")
im.save(fp, format=None)  # 保存图片

The parameters are described as follows:

  • fp: the storage path of the picture, including the name of the picture, in string format
  • format: optional parameter, you can specify the format of the picture

3.2 convert method

Note that not all image formats can be converted using the save() method. For example, if you save an image in PNG format to JPG format, an error will occur if you use the save() method directly

The error is thrown because of inconsistent PNG and JPG image modes. Among them, PNG is a four-channel RGBA mode, that is, red, green, blue, and Alpha transparent color; JPG is a three-channel RGB mode. Therefore, in order to achieve image format conversion, it is necessary to convert PNG to three-channel RGB mode

The convert() method provided by the Image class can realize the conversion of the image mode. This function provides multiple parameters, such as mode, matrix, dither, etc., among which the most critical parameter is mode, and other parameters do not need to be concerned

grammar:

im.convert(mode, params)  # 转换模式
im.save(fp)  # 保存图片

parameter:

  • mode: refers to the image mode to be converted to
  • params: other optional parameters

4. Image scaling

In the process of image processing, it is often encountered that the image is reduced or enlarged. The resize() method provided by the Image class can realize arbitrary reduction and enlargement of the image.

grammar:

im_new = im.resize(size, resample=image.BICUBIC, box=None, reducing_gap=None)  # 注意要重新赋值
im_new.show()  # 缩放后的图片

parameter:

  • size: tuple parameter (width, height), the size of the image after scaling

  • resample: an optional parameter, which refers to the image resampling filter, similar to the resample parameter of thumbnail(), and defaults to Image.BICUBIC

  • box: pair specifiedpicture areaFor scaling, the parameter value of box is a tuple of pixel coordinates with a length of 4, ie (left, top, bottom right). Note that the specified area must be within the range of the original image, and an error will be reported if it exceeds the range. When this parameter is not passed, the entire original image is zoomed by default

    (0, 0, 120, 180) represents the origin of the upper left corner of the original image, and selects the image area whose width and height are (120,180) respectively

  • reducing_gap: optional parameter, floating point parameter value, used to optimize the zoom effect of the image, common parameter values ​​are 3.0 and 5.0

5. Create thumbnails

Thumbnail refers to reducing the original image to a specified size (size). Make images easier to display and browse by creating thumbnails

The Image object provides a thumbnail() method to generate a thumbnail of the image, which is scaled proportionally

grammar:

im.thumbnail(size,resample)  # 直接在原图的基础上修改
im.show()  # 缩放后的图片

parameter:

  • size: tuple parameter, refers to the reduced image size
  • resample: an optional parameter, which refers to the image resampling filter. There are four filtering methods, namely Image.BICUBIC (Bicubic Interpolation), PIL.Image.NEAREST (Nearest Neighbor Interpolation), PIL.Image.BILINEAR (Dual Linear interpolation method), PIL.Image.LANCZOS (downsampling filter interpolation method), the default is Image.BICUBIC

6. Image separation and merging

An image (referring to a digital image) is composed of many pixels. A pixel is the basic unit of an image, and each pixel can use a different color, and finally presents a colorful image. The separation and merging of an image refers to It is the separation and combination of image colors

6.1 split method

im = PIL.Image.open(r"D:\35005\Pictures\Screenshots\微信图片_20220302175157.jpg")
r, g, b = im.split()  # split 方法使用较简单,分离通道
r.show()
g.show()
b.show()

6.2 The merge method

The merge() method provided by the Image class can realize the merge operation of images. Note that image merging can be a single image merging or more than two images

im_merge = PIL.Image.merge(mode, bands)
im_merge.show()

parameter:

  • mode: Specifies the mode of the output image
  • bands: The parameter type is a tuple or a list sequence, and its element value is the color channel that makes up the image. For example, RGB represents three color channels, which can be expressed as (r, g, b)

6.3 The blend method

The Image class also provides the blend() method to mix images in RGBA mode (PNG format)

grammar:

PIL.Image.blend(image1,image2, alpha)

parameter:

  • image1: image object 1
  • image2: image object 2
  • alpha: transparency, the value range is 0 to 1, when the value is 0, the output image is equivalent to a copy of image1, and when the value is 1, it is a copy of image2, only when the value is 0.5, it is Merge of two images. Therefore, the size of the value determines the degree of mixing of the two images

7. Image processing

7.1 Image cropping

The crop() function provided by the Image class allows us to crop the original image in a rectangular area

grammar:

im_crop = im.crop(box=None)  # box 代表裁剪区域
im_crop.show()

box is a tuple parameter with four numbers (x_top left, y_bottom left, x1_upper right, y1_bottom right), respectively representing the x, y coordinates of the upper left corner and the x, y coordinates of the lower right corner of the clipped rectangular area . The default (0,0) represents the origin of the coordinates, the direction of the width is the x-axis, the direction of the height is the y-axis, and each pixel represents a unit

7.2 Copy and Paste

Copy and paste operations almost appear in pairs, and the Image class provides copy() and paste() methods to implement image copy and paste

Copy syntax:

im_copy = im.copy()  # 复制图片

Paste syntax:

im_copy.paste(image, box=None, mask=None)

parameter:

  • image: Refers to the pasted image
  • box: Specify the position or area where the picture is pasted. Its parameter value is a sequence of tuples with a length of 2 or 4. When the length is 2, it means a specific point (x, y); if the length is 4, it means the area where the picture is pasted. , at this time the size of the area must be consistent with the size of the pasted image
  • mask: optional parameter, add a mask effect to the image

Notice:

  • The pasted picture mode will be automatically consistent, no additional conversion is required
from PIL import Image

im = Image.open(r"D:\35005\Pictures\Screenshots\微信图片_20220302175157.jpg")
# 复制一张图片副本
im_copy = im.copy()
# 对副本进行裁剪
im_crop = im_copy.crop((0, 0, 200, 100))
# 创建一个新的图像作为蒙版,L模式,单颜色值
image_new = Image.new('L', (200, 100), 200)
# 将裁剪后的副本粘贴至副本图像上,并添加蒙版
im_copy.paste(im_crop, (100, 100, 300, 200), mask=image_new)
# 显示粘贴后的图像
im_copy.show()

8. Geometric changes

The geometric transformation of an image mainly includes image flipping, image rotation and image transformation operations, and the Image class provides functions transpose(), rotate() and transform() to handle these operations

8.1 transpose

This function can realize the vertical and horizontal flipping of the image

grammar:

im_out = im.transpose(method)  # 生成新的图像对象

method value:

  • Image.FLIP_LEFT_RIGHT: Flip left and right horizontally
  • Image.FLIP_TOP_BOTTOM: Flip vertically up and down
  • Image.ROTATE_90: Rotate the image 90 degrees counterclockwise
  • Image.ROTATE_180: Rotate the image 180 degrees
  • Image.ROTATE_270: Rotate the image 270 degrees
  • Image.TRANSPOSE: Image transpose
  • Image.TRANSVERSE: flip the image horizontally

8.2 rotate

When we want to rotate the image at any angle, we can use the rotate() function

grammar:

im_out = im.rotate(angle, resample=PIL.Image.NEAREST, expand=None, center=None, translate=None, fillcolor=None)  # 返回图像对象

parameter:

  • angle: Indicates the angle of any rotation
  • resample: resampling filter, the default is PIL.Image.NEAREST nearest neighbor interpolation method
  • expand: optional parameter, indicating whether to expand the image, if the parameter value is True, the output image will be expanded, if it is False or omitted, it will output according to the original image size
  • center: optional parameter, specify the center of rotation, the parameter value is a tuple of length 2, the default is to rotate at the center of the image
  • translate: The parameter value is a two-tuple, which means to translate the rotated image, with the upper left corner as the origin; the parameter value of translate can be negative
  • fillcolor: optional parameter, fill color, after the image is rotated, fill the area outside the image

8.3 transform

This function can transform the image, and generate a new image of a specified size through the specified transformation method.

grammar:

im_out = im.transform(size, method, data=None, resample=0)  # 返回图像对象

parameter:

  • size: Specifies the size of the new image
  • method: Specifies how the image changes, such as Image.EXTENT means rectangular transformation
  • data: This parameter is used to provide the required data for the transformation method
  • resample: image resampling filter, the default parameter value is PIL.Image.NEAREST

Three, ImageFilter

1 Introduction

With the continuous development of digital image technology, image noise reduction methods are becoming more and more mature. Constructing filters through certain algorithms is the main way of image noise reduction. The filter can effectively suppress the generation of noise without affecting the shape, size and original topology of the processed image

Pillow achieves the purpose of image noise reduction through the ImageFilter class. This class integrates different types of filters. By calling them, image noise reduction operations such as image smoothing, sharpening, and boundary enhancement can be achieved.

2. Noise reduction processing

2.1 Image noise reduction filter

nameillustrate
ImageFilter.BLURFuzzy filtering, that is, mean filtering
ImageFilter.CONTOURContour filtering, looking for image contour information
ImageFilter.DETAILDetail filtering makes the image display finer
ImageFilter.FIND_EDGESFind boundary filtering (find the boundary information of the image)
ImageFilter.EMBOSS浮雕滤波,以浮雕图的形式显示图像
ImageFilter.EDGE_ENHANCE边界增强滤波
ImageFilter.EDGE_ENHANCE_MORE深度边缘增强滤波
ImageFilter.SMOOTH平滑滤波
ImageFilter.SMOOTH_MORE深度平滑滤波
ImageFilter.SHARPEN锐化滤波
ImageFilter.GaussianBlur()高斯模糊
ImageFilter.UnsharpMask()反锐化掩码滤波
ImageFilter.Kernel()卷积核滤波
ImageFilter.MinFilter(size)最小值滤波器,从 size 参数指定的区域中选择最小像素值,然后将其存储至输出图像中。
ImageFilter.MedianFilter(size)中值滤波器,从 size 参数指定的区域中选择中值像素值,然后将其存储至输出图像中。
ImageFilter.MaxFilter(size)最大值滤波器
ImageFilter.ModeFilter()模式滤波

2.2 使用语法

语法:

im_ft = im.filter(filt_mode)  # 返回图像对象,里面传入滤波器

实例:

from PIL import Image, ImageFilter

im = Image.open(r"D:\35005\Pictures\Screenshots\微信图片_20220302175157.jpg")
im_ft = im.filter(ImageFilter.EMBOSS)  # 添加浮雕滤波器
im_ft.show()

相当于PS里面添加的滤镜

四、 ImageColor

1、 简介

Pillow 提供了颜色处理模块 ImageColor,该模块支持不同格式的颜色,比如 RGB 格式的颜色三元组、十六进制的颜色名称(#ff0000)以及颜色英文单词(“red”)。同时,它还可以将 CSS(层叠样式表,用来修饰网页)风格的颜色转换为 RGB 格式

在 ImageColor 模块对颜色的大小写并不敏感,比如 “Red” 也可以写为 " red"

2、 颜色处理

2.1 颜色命名

ImageColor 支持多种颜色模式的的命名(即使用固定的格式对颜值进行表示),比如我们熟知的 RGB 色彩模式,除此之外,还有 HSL (色调-饱和度-明度)、HSB (又称 HSV,色调-饱和度-亮度)色彩模式。下面对 HSL 做简单介绍:

  • H:即 Hue 色调,取值范围 0 -360,其中 0 表示“red”,120 表示 “green”,240 表示“blue”
  • S:即 Saturation 饱和度,代表色彩的纯度,取值 0~100%,其中 0 代表灰色(gry),100% 表示色光最饱和
  • L:即 Lightness 明度,取值为 0~100%,其中 0 表示“black”黑色,50% 表示正常颜色,100% 则表示白色

亮度和明度的表达方式类似,链接中有具体描述:【https://www.zhihu.com/question/22077462】

ImageColor 模块比较简单,只提供了两个常用方法,分别是 getrgb() 和 getcolor() 函数

2.2 getrgb

语法:

rgb = PIL.ImageColor.getrgb(color)  # 得到颜色的 rgb 数值

color参数即可以是英文,也可以是HSL和HSB模式2.3

应用:

from PIL import Image, ImageColor

im = Image.new(mode="RGB", size=(100, 100), color=ImageColor.getrgb('HSL(0,100%,50%)'))
im.show()

2.3 getcolor

语法:

val = PIL.ImageColor.getcolor(color, mode)

参数:

  • color:一个颜色名称,字符串格式,可以是颜色的英文单词,或者十六进制颜色名。如果是不支持的颜色,会报 ValueError 错误
  • mode:指定色彩模式,如果是不支持的模式,会报 KeyError 错误

五、 ImageFont

1、 简介

ImageFont模块定义了相同名称的类,即ImageFont类。这个类的实例存储bitmap字体,用于ImageDraw类的text()方法

PIL使用自己的字体文件格式存储bitmap字体。用户可以使用pilfont工具包将BDF和PCF字体描述器(Xwindow字体格式)转换为这种格式

2、 模块函数

2.1 load

语法:

ft = PIL.ImageFont.load(font_file)

从指定的文件中加载一种字体 ,返回字体对象

2.2 load_path

语法:

ft = PIL.ImageFont.load_path(font_file)

和函数load()一样,但是如果没有指定当前路径的话,会从sys.path开始查找指定的字体文件

2.3 truetype

语法:

ft = PIL.ImageFont.truetype(file, size[, encoding=None])

参数:

  • file: 加载一个TrueType或者OpenType字体文件
  • size: 为指定大小的字体创建了字体对象
  • encoding:字体编码,主要字体编码有: “unic”(Unicode),“symb”(Microsoft Symbol),“ADOB”(Adobe Standard),“ADBE”(Adobe Expert)和“armn”(Apple Roman)

2.4 load_default

语法:

ft = PIL.ImageFont.load_default()

加载一个默认字体,返回一个字体对象

3、 模块方法

3.1 getsize

语法:

size = ft.getsize(text)

返回给定文本的宽度和高度,返回值为2元组

3.2 getmask

语法:

obj = ft.getmask(text,mode=None)  # 为给定的文本返回一个位图。这个位图是PIL内部存储内存的实例

参数:

  • text :要渲染的文本。
  • mode:某些图形驱动程序使用它来指示驱动程序喜欢哪种模式;如果为空,渲染器可能返回任一模式。请注意,模式始终是字符串

六、 ImageDraw

1、 简介

1.1 导入

ImageDraw 模块也是 Pillow 库的主要模块之一,它能给图像化圆弧,画横线,写上文字等

导入:

from PIL import ImageDraw

实例化对象:

from PIL import Image, ImageDraw

im = Image.open("./a.jpg")  # 创建 image 对象
draw = ImageDraw.Draw(im)  # 实例化可以在指定图像上画图的 Draw 对象

1.2 基本概念

  1. Coordinates

    • 绘图接口使用和PIL一样的坐标系统,即(0,0)为左上角。
  2. Colours

    • 为了指定颜色,用户可以使用数字或者元组,对应用户使用函数Image.new或者Image.putpixel。对于模式为“1”,“L”和“I”的图像,使用整数。对于“RGB”图像,使用整数组成的3元组。对于“F”图像,使用整数或者浮点数。

    • 对于调色板图像(模式为“P”),使用整数作为颜色索引。在1.1.4及其以后,用户也可以使用RGB 3元组或者颜色名称。绘制层将自动分配颜色索引,只要用户不绘制多于256种颜色。

  3. Colours Names

    • 在PIL 1.1.4及其以后的版本,用户绘制“RGB”图像时,可以使用字符串常量。PIL支持如下字符串格式:

      • A、 十六进制颜色说明符,定义为“#rgb”或者“#rrggbb”。例如,“#ff0000”表示纯红色。

      • B、 RGB函数,定义为“rgb(red, green, blue)”,变量red、green、blue的取值为[0,255]之间的整数。另外,颜色值也可以为[0%,100%]之间的三个百分比。例如,“rgb(255, 0, 0)”和“rgb(100%, 0%, 0%)”都表示纯红色。

      • C、 HSL(Hue-Saturation-Lightness)函数,定义为“hsl(hue,saturation%, lightness%)”,变量hue为[0,360]一个角度表示颜色(red=0, green=120, blue=240),变量saturation为[0%,100%]之间的一个值(gray=0%,full color=100%),变量lightness为[0%,100%]之间的一个值(black=0%, normal=50%, white=100%)。例如,“hsl(0,100%, 50%)”为纯红色。

      • D、 通用HTML颜色名称,ImageDraw模块提供了140个标准颜色名称,Xwindow系统和大多数web浏览器都支持这些颜色。颜色名称对大小写不敏感。例如,“red”和“Red”都表示纯红色。

  4. Fonts

    • PIL可以使用bitmap字体或者OpenType/TrueType字体

2、 模块函数

2.1 arc

语法:

draw.arc(xy, start, end, options)

在给定的区域内,在开始和结束角度之间绘制一条弧

options:可以有什么内容可以在源代码中查看

2.2 bitmap

语法:

draw.bitmap(xy, bitmap, options)  # options中可以添加 fill 覆盖的颜色

在给定的区域里绘制变量bitmap所对应的位图,非零部分使用变量options中fill的值来填充。变量bitmap位图应该是一个有效的透明模板(模式为“1”)或者蒙版(模式为“L”或者“RGBA”)

变量xy是变量bitmap对应位图起始的坐标值,而不是一个区域

这个方法与Image.paste(xy, color, bitmap)有相同的功能

2.3 chord

语法:

draw.chord(xy, start, end, options)  

和方法arc()一样,但是使用直线连接起始点

The outline of the variable options specifies the color of the outline of the string; fill specifies the color of the interior of the string

2,4 ellipse

grammar:

draw.ellipse(xy, options)

draws an ellipse in the given area

The outline of the variable options specifies the color of the outline of the string; fill specifies the color of the interior of the string

2.5 line

grammar:

draw.line(xy, options)

draws lines between the coordinates represented by the variable xy list

There are at least two coordinates in xy, which are represented by tuples and stored in a list [(x1, y1), (x2, y2)]

width specifies the width, fill specifies the color of the line

2.6 pieslice

grammar:

draw.pieslice(xy, start, end, options)

Same as method arc(), but draws a straight line between the end point and the center point in the specified area

2.7 point

grammar:

draw.point(xy, options)

Draw a small dot occupying only one pixel at the specified position

2.8 polygon

grammar:

draw.polygon(xy, options)

draw a polygon

The polygon outline consists of straight lines between the given coordinates, and a straight line is added between the last and first coordinates to form a polygon

A coordinate list is any sequence object containing a 2-tuple [(x,y),…] or a number [x,y,…], which includes at least 3 coordinate values

The fill of the variable options gives the color inside the polygon

2.9 rectangle

grammar:

draw.rectangle(xy, options)

draw a long side

The variable xy is any sequence object containing 2-tuples [(x,y),…] or numbers [x,y,…], it should include 2 coordinate values

Note: when the rectangle is not filled, the second coordinate pair defines a point outside the rectangle

The fill of the variable options specifies the color inside the long side

2.10 text

grammar:

draw.text(xy, string, options)

Draws a string at the given position. The variable xy gives the position of the upper left corner of the text

The font of the variable option is used to specify the font used. It should be an instance of the class ImangFont, loaded from a file using the load() method of the ImageFont module

The fill of the variable options specifies the color of the text

2.11 textsize

grammar:

draw.textsize(string, options)

Returns the size of the given string in pixels

The font of the variable option is used to specify the font used. It should be an instance of the class ImangFont, loaded from a file using the load() method of the ImageFont module

7. Image and Numpy

from PIL import Image
import numpy as np

im = Image.open("./a.jpg")
print(np.asarray(im))  # 三维数组
na = np.asarray(im)  # 将图片转换为数组
na[0][0][0] = 0  # 修改数组的值
im_new = Image.fromarray(na)  # 将数组转换为图片


Category of website: technical article > Blog

Author:mmm

link:http://www.pythonblackhole.com/blog/article/79855/d45fb8be62a294742d51/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

13 0
collect article
collected

Comment content: (supports up to 255 characters)