Pillow Python image processing library using the conventional method

PIL (Python Imaging Library) Python is a powerful and convenient image processing library, only supports Python2.7.
Pillow PIL is a descendant branch, Pillow PIL in place by Python3 standard library.
Pillow official website: https://pillow.readthedocs.io/en/latest/handbook/index.html

Here is an example of usage visible code comments.

from the PIL Import Image, ImageFilter, ImageGrab, ImageDraw, ImageFont 

# Create Image: width 800 * height 600, red 
imNew = Image.new ( ' the RGB ' , (800,600), (255 , 0,0)) 

# display pictures 
# imNew .Show () 

# screen capture 
imGrab = ImageGrab.grab () 
imGrab.save ( ' grab.jpg ' , ' jpeg ' ) 

# open the picture 
IM = Image.open ( ' 1.jpg ' ) 

# copy images 
IM1 = IM .copy () 
IM2 = im.copy () 
IM3= Im.copy () 
IM4 = im.copy () 
IM5 = im.copy () 
IM6 = im.copy () 
IM7 = im.copy () 

# obtain high image width: 
W, H = im.size
 Print ( ' image width and height: {} * {} ' .format (W, H)) 

# thumbnail (image will not be stretched, only reduced) 
im.thumbnail ((W 2 //, H // 2 )) 
IM .save ( ' 1_thumbnail.jpg ' , ' JPEG ' ) 

# scaling (image may be stretched, can be reduced to enlarge) 
IM1 im1.resize = ((W 2 //, H // 2 )) 
im1.save ( '1_resize.jpg ' , ' JPEG ' ) 

# blurred image 
IM2 = im2.filter (ImageFilter.BLUR) 
im2.save ( ' 1_blur.jpg ' , ' JPEG ' ) 

# rotate the image rotated 45 degrees counterclockwise 
im3 = im3.rotate (45 ) 
im3.save ( ' 1_rotate.jpg ' , ' JPEG ' ) 

# image conversion: conversion around FLIP_LEFT_RIGHT, down conversion FLIP_TOP_BOTTOM 
IM4 = im4.transpose (Image.FLIP_LEFT_RIGHT) 
im4.save ( ' 1_transpose.jpg ' , 'jpeg' ) 

# Image cropping 
Box = (200,200,400,400) # top left corner (0,0), the 4-tuple coordinates indicating positions: left, top, right, bottom 
IM5 = im5.crop (Box) 
im5.save ( ' 1_crop.jpg ' , ' jpeg ' ) 

# add text on picture 
Draw = ImageDraw.Draw (IM6)
 # TrueType set the font, text size 
# stxingka.ttf Chinese Xingkai simkai.ttf italics simli.ttf official script 
font = ImageFont.truetype ( " C: \\ stxingka.ttf Fonts \\ \\ WINDOWS " , 20 ) 
draw.text (( 100, 100), ( ' the hello Word \ the n-hello, world ' ), the Fill = ' # 0000FF' , = Font font) 
im6.save ( ' 1_drawText.jpg ' , ' JPEG ' ) 

# Add image (pasted image) pictures 
imTmp = Image.new ( ' the RGB ' , (30, 30), ' Blue ' ) 
IM7 .paste (imTmp, ( 50,50)) # the second parameter to the coordinate 
im7.save ( ' 1_paste.jpg ' , ' JPEG ' ) 


# image transverse splicing: splicing above im6, im7 (two same size picture) 
im6Width , im6Height = im6.size 
imHorizontal = Image.new ( ' the RGB ', (im6Width * 2, im6Height))
imHorizontal.paste(im6, (0,0))
imHorizontal.paste(im7, (im6Width,0))
imHorizontal.save('1_horizontal.jpg', 'jpeg')

# 图片竖向拼接:拼接上面im6、im7
imVertical = Image.new('RGB', (im6Width, im6Height*2))
imVertical.paste(im6, (0,0))
imVertical.paste(im7, (0,im6Height))
imVertical.save('1_vertical.jpg', 'jpeg')

 

Guess you like

Origin www.cnblogs.com/gdjlc/p/11444132.html