Use of Python PIL library

PIL library overview

The PIL library is an excellent third-party library for Python. It needs to be installed via pip:

pip install pillow 

The PIL library supports image storage, display, and processing. It can process almost all picture formats, and can complete operations such as zooming, cropping, superimposing, and adding lines, images, and text to the image.

PIL library Image class analysis

In PIL, any image file can use the Image object to represent the image reading and creating methods of the Image class:

Image.open (filename)     # according to the parameters load image files 
Image.new (the MODE, size, Color)     # according to create a new image of a given parameter 
Image.open (StringIO.StringIO (Buffer))     # acquire images from a string 
Image.frombytes (MODE, size, Data)     # creates an image according to the pixel Data 
Image.verify ()     # image file integrity check returns an exception

The Image class has 4 common attributes for processing pictures:

Image.Format     # logo image format or source, if the image is not read from a file, the value is None 
Image.mode     # color mode image, "L" grayscale image, "RGB" true color images, "CMYK" Publishing image 
Image .size     # image width and height in pixels (PX), the return value is the two tuple (tuple) 
Image.palette     # palette property, a return type ImagePalette

There are three conversion and saving methods for Image pictures:

The Image.Save (filename, format)     # Save the image as filename file name, format is the picture format 
Image.convert (the MODE)      # using different parameters, convert the image into a new mode 
Image.thumbnail (size)      # create shrink image Thumbnail, size is a binary tuple of thumbnail size

Image class image zoom and rotation methods:

Image.resize (size) #Resize the image according to size, generate a copy
Image.rotate (angle) #rotate the image by angle, generate a copy

The Image class can operate on each pixel or each channel of an RGB image separately. The split () method can extract each color channel of the RGB image, and the merge () method can synthesize each independent channel into a new one. image:

Image.point (func)     # calculates each element according to the function func function, returns a copy of the image 
Image.split ()     # extract each color channel of the RGB image, an image copy of the return 
Image.merge (MODE, bands Scrap)     # combined channel , Using mode color, bands is the color channel of the new color 
Image.blend (im1, im2, alpha)

Image.blend (im1, im2, alpha) interpolates the two images im1 and im2 according to the following formula to generate a new image: im1 * (1.0-alpha) + im2 * alpha

 

The ImageFilter and ImageEnhance classes of the PIL library provide methods for filtering and enhancing images, a total of 10 types:

ImageFilter.BLUR     # blur images 
ImageFilter.CONTOUR      # contour effect of the image 
ImageFilter.DETAIL     # details of the image effect 
ImageFilter.EDGE_ENHANCE     # border of the image to enhance the effect 
ImageFilter.EDGE_ENHANCE_MORE     # image threshold boundary strengthening effect 
ImageFilter.EMBOSS     # image of relief effect 
ImageFilter.FIND_EDGES     # boundary effect of the image 
ImageFilter.SMOOTH      # smoothing effect of an image 
ImageFilter.SMOOTH_MORE     # threshold image smoothing effect 
ImageFilter.SHARPEN      # sharpening image

The ImageEnhance class provides more advanced image enhancement requirements. It provides functions such as adjusting color, brightness, contrast, and sharpening:

ImageEnhance.enhance (factor)     # value of the selected property enhancement factor times 
ImageEnhance.Color (IM)     # adjust image color balance 
ImageEnhance.Contrast (IM)     # adjust the contrast of image 
ImageEnhance.Brightness (IM)     # Adjusting the brightness 
ImageEnhance (IM) .Sharpness     # to adjust image sharpness

 

 

Get thumbnail

 

 

from PIL import Image
im = Image.open("C:\\bachongying.jpg")
im.thumbnail((128,128))
im.save('1bachongying.jpg')

 

 

Get picture outline

 

from PIL import Image
from PIL import ImageFilter
im = Image.open("C:\\bachongying.jpg")
om = im.filter (ImageFilter.CONTOUR)
om.save ( ' bachongying2.jpg ' )

  Picture embossed

from PIL import Image
from PIL import ImageFilter
im = Image.open("C:\\bachongying.jpg")
about = im.filter (ImageFilter.EMBOSS)
om.save ( ' bachongying3.jpg ' )

 

 

Make a Chinese character painting

 

 

 

from PIL import Image
ascii_char   = list ( ' Learning makes me happy ' )
 def get_char (r, b, g, alpha = 256 ):
     if alpha == 0:
         return  '  ' 
    gray = int (0.2126 * r + 0.7152 * g + 0.0722 * b)
    unit = 256 / len(ascii_char)
    return ascii_char[int(gray//unit)]
def main():
    im = Image.open('C:\\bachongying.jpg')
    WIDTH, HEIGHT = 100, 60
    im = im.resize((WIDTH, HEIGHT))
    txt = ""
    for i in range(HEIGHT):
        for j in range(WIDTH):
            txt += get_char(*im.getpixel((j, i)))
        txt += '\n'
    fo = open("pic_char.txt","w")
    fo.write(txt)
    fo.close()
main()

 

 

Guess you like

Origin www.cnblogs.com/lulingboke/p/12702692.html