Python image processing, the Pillow library is really powerful (17)

Hello children, hello big friends!

I'm Cat Girl, a primary school student who fell in love with Python programming.

Welcome to learn Python with cat girl.

today's topic

Today we learn how to process images with Python.

What is the library we use today?

It is the famous Pillow.

Pillow is a third-party library that needs to be installed with pip (pip install pillow).

It's easy to use and powerful.

Basic knowledge

color value

When it comes to images, colors are inseparable. How to express colors in Python?

In Python, RGB or RGBA are generally used to represent.

RGB is a color standard that uses the three primary colors of Red, Green, and Blue to obtain various colors.

RGBA adds opacity A (Alpha) on the basis of RGB.

Python uses tuples to represent colors, such as (R,G,B) or (R,G,B,A).

The former is RGB and the latter is RGBA.

The value range is 0~255, A=0 means completely transparent, A=255 means completely opaque.

The English names of commonly used colors and the corresponding RGB are as follows:

  • while:(255,255,255)

  • black:(0,0,0)

  • red:(255,0,0)

  • green:(0,128,0)

  • blue:(0,0,255)

  • yellow:(255,255,0)

  • gray:(128,128,128)

  • purple:(128,0,128)

pixel

A pixel refers to the basic unit of an image, that is, a point.

An image is composed of many small squares, each of which is a pixel.

For example, we usually say that the display resolution is 800 pixels * 600 pixels, that is, the screen consists of small squares with a width of 800 and a height of 600.

Coordinate System

To locate a pixel on the screen, a coordinate system is used.

The coordinate system we often see is the mathematical coordinate system, but Pillow uses the image coordinate system.

The only difference between the two is the positive direction of the y-axis.

Mathematical coordinate systems are generally used in mathematics, and most of Python uses image coordinate systems.

image manipulation

open the picture

from PIL import Image

img=Image.open("file path")

We first import the Pillow library, then open an image file, and the return value is an Image object.

We can use the properties and methods of the Image object to operate on the image.

The properties are as follows

  • filename: picture name

  • format: image format

  • size: image size in bytes

Methods as below

  • show(): display pictures

  • save(): error picture

  • resize(): change the image size

  • crop(): cut the picture

  • rotate(): rotate the picture

  • transpose(): Flip the picture

  • copy(): Copy the picture

  • paste(): Paste the picture

Let's quickly test it out!

Image Processing

Get image information:

display image:

save Picture:

change size:

Cut the picture:

Rotate image:

Flip the picture:

Copy and paste paste pictures:

Create zones:

The syntax is: Image.new("RGB",(x,y),color)

The first parameter is the color mode, which can be set to RGB or RGBA

The second parameter tuple, x represents the width, y represents the height

The third parameter color value or color transparency value

The return object is an Image object, both methods Image.open() and Image.new() will return an Image object.

draw graphics

The ImageDraw module in Pillow can draw various graphics.

Create a Draw() object through the Draw() method of the ImageDraw module, and then draw various graphics based on it.

point:

straight line:

rectangle:

You can also draw polygons, arcs, sectors, circles, ellipses, etc.

There are no pictures of cat girls here, they are all similar.

draw text

The ImageDraw module in Pillow can draw text.

You can refer to our previous article!

"Hurricane" wallpaper sister-in-law is so amazing, make it into a calendar wallpaper every day (7)

Finally, let's introduce ImageFront.truetype(url,size)

url: Indicates the path where the font file is located, and its extension is usually ttf.

size: Indicates the number of points (not pixels) of the font size, which is an integer.

The PNG image created by the Pillow library defaults to 72 pixels per inch (1 inch is approximately 2.54 cm), so 1 point is 1/72 pixel.

Note that the ImageFilter above can beautify the picture, and it has various filter effects.

ImageFilter.properties

ImageFilter. method name()

Well, let's learn this today!

If you encounter any problems, let's communicate and solve them together.

I'm Cat Girl, see you next time!

Guess you like

Origin blog.csdn.net/parasoft/article/details/129773189