Python on image processing PIL

Python PIL

The PIL (Python Image Library) library is a third-party library of the Python language. The PIL library supports image storage, display, and processing, and can handle images in almost all formats.

  1. Introduction to PIL library

  2. The PIL library has two main functions:

(1) Image filing: batch processing of images, production image preview, image format conversion, etc.

(2) Image processing: basic image processing, pixel processing, color processing, etc.

  1. PIL has multiple classes. Here is a brief introduction to the Image class, ImageFilter class, and ImageEnhance class.

Second, install library functions

Three, use the library function Image class-basic image processing

  1. Call the Image class
from PIL import Image
  1. Image class about image reading and creation
Method Description
Image.open(filename) Load image file
Image.new(mode,size,color) Create a new image according to the given parameters
Image.open(StringIO.StringIO(buffer)) Get image from string
Image.frombytes(mode,size,color) Create a new image based on pixels
Image.verify() Check the integrity of the image and return an exception
  1. Common attributes of the Image class
Method Description
Image.format Image format or source, if the image is not read from a file, return None
Image.mode The color mode of the image,'L' is grayscale mode,'RGB' is true color image,'C (cyan) M (magenta) Y (yellow) K (black)' is the published image
Image.size The width and height of the image, in pixels (px), the return value is a tuple type
Image.palette Palette property, return ImagePalette type

  1. Sequence image operation method of Image class
Method Description
Image.seek(frame) Jump and return to the specified frame in the image
Image.tell() Returns the serial number of the current frame
  1. Image rotation and scaling methods of the Image class
Method Description
Image.resize(size) return a copy of the image resized by size
Image.rotate(angle) Returns a copy of the image rotated by angle

  1. Image conversion and saving method of Image class
Method Description
Image.save(filename,format) Save the image as filename, format format
Image.convert(mode) Convert the image to mode mode
Image.thumbnail(size) Create a thumbnail of the image, size is a tuple of thumbnail size

- - encoding:utf-8 --

''' Change the color --- color inversion'''
from PIL import Image
nest = Image.open("D:\nest.jpg")
r,g,b = nest.split() #Get the color value of the RGB channel of the original image
newb = b.point(lambda i:i*1.1) #Enhance the color value of the B channel
nest1 = Image.merge(nest.mode,(b,g,r))
nest1.thumbnail((400,254)) #Create thumbnail
nest1.save("D:\nest_2.jpg")

  1. Image pixel and channel processing method of Image class
Method Description
Image.point(func) Calculate each element according to the function of the function func, and return a copy of the image
Image.split() According to each color channel of the GRB image, return a copy of the image
Image.merge(mode,bands) composite channel, where mode is color, bands is the new color channel
Image.blend(im1,im2,alpha) Interpolate the two pictures im1 and im2 according to the formula to generate a new image formula: im1*(1.0-alpha) + im2*alpha

Fourth, use the library function ImageFilter class-image filtering

  1. Call the ImageFilter class
from PIL import ImageFilter
  1. ImageFilter class predefined image filtering method
Method Description
ImageFilter.BLUR Image blur effect
ImageFilter.CONTOUR Image contour effect
ImageFilter.DETAIL Image detail effect
ImageFilter.EDGE_ENHANCE Image boundary enhancement effect
ImageFilter.EDGE_ENHANCE_MORE Image threshold boundary enhancement effect
ImageFilter.EMBOSS Image relief effect
ImageFilter.FIND_EDGES Image border effect
ImageFilter.SMOOTH Image smoothing effect
ImageFilter.SMOOTH_MORE Image threshold smoothing effect
ImageFilter.SHARPEN Image sharpening effect
# - *- encoding:utf-8-*-'''Contour effect---sketch'''from PIL import Image
from PIL import ImageFilter
square = Image.open("D:\\square.jpg")
square1 = square.filter(ImageFilter.CONTOUR)  #Select contour effect
square1.save("D:\\square0.jpg")

Recommended Posts

Python on image processing PIL
Python PIL library image graying processing
python PIL open\display\save image
python_ file processing
Python processing json summary
Python image recognition OCR
Install Python3 on Ubuntu 14.04
Python implements image stitching
Install Python3 on Ubuntu 16.04
Install Python3.7 on Ubuntu
python Tic-Tac-Toe-text version (on)
Python datetime processing time summary
Install Python 3.7 on Ubuntu 18.04 LTS
Install python3 on linux, keep python2
Python implements panoramic image stitching
python opencv for image stitching
Python implements image stitching function
Python introductory notes [basic grammar (on)]
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
Python processing PDF and CDF examples
Python realizes image recognition car function
Install Python virtual environment on Ubuntu 18.04
Configure python3 environment on centos7 and
Install TensorFlow (python2.7 version) on Ubuntu
How Python operates on file directories