Image module of pilot

Functions in Image module

The return value of the function in the Image module is an Image object, so you can create an instance of the Image class through the function in the module.

1.open

Image.open(file, mode)
  • file                         
  • mode                         
  • returns an Image object

Function: load the image with full path as file by mode

Note: after creating an instance of the Image class, you can display the Image through the class's method show().

Example: open an image and display

 #Import Image module
from PIL import Image  
#Open the image named 'image.jpg'             
img = Image.open('image.jpg')        
#display
img.show()                           

Operation result:
Call the picture viewer and open the image named "image.jpg" under the file directory.

2.new

Image.new(mode, size, color=0)
  • mode                             
  • Size   image size to create
  • color                                      
  • returns an Image object

Function: create an image with mode, size and color.

Example: create an image with RGB mode, 400x200 resolution and red color

#Import Image module
from PIL import Image
#Create an image with RGB mode, 400x200 resolution and red color
img_1 = Image.new('RGB',(400,200),(255,0,0))  #Color determination by tuple
img_2 = Image.new('RGB',(400,200),'red')      #Color is determined by color characters in ImageColor module
# Display image
img_1.show()
img_2.show()

Operation result:

3.blend

Image.blend(im1, im2, alpha)
  • im1                             
  • im2                             
  • alpha                           
  • returns an Image object

Function: mix im1 and im2 images (both must have the same mode and size) with certain transparency;
Mixed method: (im1 x (1-alpha) + im2 x alpha)
Special case:

  • When alpha is 0, returns a copy of the im1 image
  • When alpha is 1, returns a copy of the im2 image

Example:

#Import Image module
from PIL import Image
#Open image im1
im1 = Image.open('im1.png')
#Open image im2
im2 = Image.open('im2.png')
#By mixing the two images with alpha=0, the original image of im1 is obtained
img0 = Image.blend(im1,im2,0)
img0.show()
#By mixing the two images with alpha=1, the original image of im2 is obtained
img1 = Image.blend(im1,im2,1)
img1.show()
#By mixing the two images with alpha=0.5, a new image object img is obtained
img = Image.blend(im1,im2,0.5)
img.show()

Operation result:

4.composite

Image.composite(im1, im2, mask)
  • im1                           
  • im2                           
  • Mask                  mixed mask
  • returns an Image object

Function: mix im1 and im2; with mask, the image mode as mask can be "1", "L" and "RGBA" (their mode and size must be the same, and mask should have certain transparency).

Example:

#Import Image module
from PIL import Image
#Open image im1
im1 = Image.open('im1.png')
#Open image im2
im2 = Image.open('im2.png')
#Open image im3
mask = Image.open('im3.png')
#Mix im1 and im2 with image im3 as mask
img = Image.composite(im1,im2,mask)
img.show()

Operation result:

Note: the key of this function is the selection of image. As a mask, the image must be transparent, or the effect will not be seen after running.

5.eval

Image.eval(image, function)
  • Image           input image
  • function                        
  • returns an Image object

Functions:
   use the function corresponding to the variable function (this function only allows to receive an integer type parameter) to process each pixel in the image represented by the variable image. If the image represented by the variable image has multiple channels, the function corresponding to the variable function will act on each channel.
Example:

#Import Image module
from PIL import Image
#Open image.jpg image
img = Image.open('image.jpg')
# Define a function
def func(x):
    return x*2
# Operation in func for each pixel of image im
img_new = Image.eval(img, func)
img_new.show()

Operation result:

6.merge

Image.merge(mode, bands)
  • Mode                   output image mode
  • bands                               
  • returns an Image object

Example:

#Import module Image
from PIL import Image
#Open images im1 and im2
im1 = Image.open("im1.png")
im2 = Image.open("im2.png")
#Separate image im1 and im2 channels
r1,g1,b1,a1 = im1.split()
r2,g2,b2,a2 = im2.split()
#Synthesize new images
im_merge = Image.merge("RGB",[r1,g2,b2])
im_merge.save("img_new.png")

Operation result:

Published 1 original article, praised 0 and visited 21
Private letter follow

Posted on Mon, 10 Feb 2020 22:37:33 -0800 by hesyar