The second Python image processing module pil (pillow)

Source: Internet
Author: User
Tags python list

This article contains: 16, point 17, Putalpha 18, PutData 19, Putpalette 20, Putpixel 21, quantize 22, Resize 23, Rotate 24, Save 25, Seek 26, Show 27, Split 28, tell 29, Thumbnail 30, Tobitmap 31, Transform 32, transpose 33, Verify

16. Point

define 1:im.point (table)? Image Im.point (function)? image meaning 1: Returns a copy of the image pixel value corresponding to the given lookup table. The variable table sets 256 values for each channel of the image. If you use a variable function, its corresponding function should have a parameter. This function will be used once for each pixel value, and the resulting table will be applied to all channels of the image. If the pattern of the image is "I (integer)" or "F (floating point)", the user must use the function mode, and the function must be in the following format: Argument* Scale+offset For example: out= Im.point (LambdaI:I * 1.2 + 10the user can omit the variable scale and offset. Example 1: fromPILImportImageim= Image.open ("Jing.png") Im_point= Im.point (LambdaX:x*1.3+5) Im_point.save ("He.png"Note: The image Im_point_fun is a lot more brightness than the original im01, because the lambda expression adds to the value of each pixel of the original. Define 2:im.point (Table,mode)? Image Im.point (function, mode)? Image meaning 2: Same as definition 1, but it specifies a new pattern for the output image. This method allows you to convert an image of the pattern "L" and "P" into a pattern of "1"image. Example 2: fromPILImportImageim= Image.open ("Jing.png") R,g,b=im.split () im_point1= R.point (LambdaX:x*1.3+5,"1")Print(Im_point1.getpixel ((0,0))) Im_point1.save ("He1.png") Im_point2= R.point (Lambdax:0,"1") Im_point2.save ("He2.png")Print(Im_point2.getpixel (10,10)))
The output is:
6
0
Image im_point1 for full Bai Tutu like Im_point2 for full black image

17, Putalpha

definition: Im.putalpha (band) Meaning: copies the given channel to the alpha layer of the image. The image mode here must be "RGBA" and the variable band must be "L" or "1". (NewinchPIL 1.1.5The Method Putalpha () can also be used in other modes, where the image is converted in situ to a pattern with an alpha channel (usually converted to "LA" or "RGBA"). The variable band is either an image or a color value (an integer). Example: fromPILImportImageim=image.open ("Xiao.png") Im1.modeim1.putalpha (100)Print(Im1.mode)Print(Im1.getpixel ((0,0))) Output:'RGB''RGBA'(11, 113, 198,100)

18, PutData

definition: Im.putdata (data) im.putdata (data, scale, offset) meaning: Copy the data from the sequence object to the current image, starting at the upper-left (0,0) position of the image. Variable scale and offset are used to adjust the value in sequence: Pixel= value *scale +offset If the variable scale is ignored, the default is 1.0. If the variable offset is ignored, the default is 0.0. Example: fromPILImportImageim=image.open ("Jing.png") R,g,b=Im.split ()Print(R.getpixel ((0,0)))Print(R.getpixel (1, 0)))Print(R.getpixel (2, 0)))Print(R.getpixel (3, 0))) R.putdata ([1,2,3,4])Print(R.getpixel ((0,0)))Print(R.getpixel (1, 0)))Print(R.getpixel (2, 0)))Print(R.getpixel (3, 0))) Output:1110961234

19, Putpalette

  definition: Im.putpalette (Sequence) meaning: Add a palette for "P" or "L" images. For an "L" image, its mode will change to "P". The palette sequence needs to contain 768 integers, each set of three values representing the corresponding pixels of red, green, and blue. The user can use a 768 byte string instead of the integer sequence. 
Example: from PIL import Imageim =image.open ( " jing.png " Span style= "COLOR: #000000" >) r,g,b = Im01.split () print (R.mode) R.putpalette ([ 1,2,3]) print (r.mode) output: '

20, Putpixel

definition: Im.putpixel (xy,colour) Meaning: Modifies the pixel value at the specified position. For a single-channel image, the variable colour is a numeric value, and for a multichannel image, the variable colour is a tuple. Note: This method performs relatively slowly. If it is 1. 1. Version 6, the pixel Access object (refer to the Load method) provides a faster way to modify the image.     If the user wants to create an entire image, you can use a more efficient method to produce a Python list, and then use the method PutData () to copy it to the image. For larger changes, use the method paste or Imagedraw module. Example:from import=image.open ("jing.png")  Print(im.getpixel (0,0)) Im.putpixel ((0,0), (+))print( Im.getpixel (0,0)) output: (one, 113, 198) (1, 2, 3)

21, Quantize

Definition: im.quantize (colors,**options)? Image meaning: (disapproval) converts an "L" or "RGB" image to a "P" image using a given color, returning a new image. In the new code, use the Convert method with an adaptive palette instead: Out =im.convert ("P", palette=image.adaptive,colors=256) 

22, Resize

Definition: im.resize (size)? Image     Im.resize (size, filter)? Image meaning: Returns a copy of the image that changed size. The variable size is the required size and is a two-tuple: (width, height).     the variable filter is one of nearest, BILINEAR, Bicubic, or AntiAlias. If omitted, or if the image mode is "1" or "P", the variable is set to nearest. Note: In the current version, the bilinear and bicubic filters do not fit well with large-scale down-sampling (for example, generating thumbnails). Users need to use AntiAlias unless speed is more important than quality. 

23, Rotate

definition: im.rotate (angle)? Image Im.rotate (Angle,filter=nearest, expand=0)? Image meaning: Returns a copy of the images rotated around the center of the image at a given angle. The variable filter should be one of nearest, bilinear, or bicubic. If the variable is omitted, or the image mode is "1"or" P ", the default is nearest. The variable expand, if true, indicates that the output image is large enough to load the rotated image. If False or default, the output image is as large as the size of the input image. Example: fromPILImportImageim= Image.open ("Jing.png") im_30= Im.rotate (30) Im_30.save ("He1.png") im_31= Im.rotate (30,image.bicubic,1) Im_31.save ("He2.png")
Note: im_30 rotation 30 degree size unchanged, im_31 rotation 30 degrees size change

24. Save

Definition: Im.save (outfile,options ...)     Im.save (outfile, format, Options ...) Meaning: Saves the image with the given file name. If the variable format defaults, if possible, the format of the file is judged from the file name extension. The method returned as empty.     the keyword options provide some additional instructions for the file writer. If the writer does not recognize an option, it ignores it.     users can use file objects in place of file names. In this case, the user must specify the file format. The file object must implement the Seek (), tell (), and write () methods, and it opens in binary mode.     if the method save () fails for some reason, this method will produce an exception (usually a IOError exception). If an exception occurs, it is possible that the method has already created the file and written some data to the file. If necessary, the user's application can delete this incomplete file.

25. Seek

definition: Im.seek (frame) Meaning: finds the specified frame in the given file sequence. If the lookup goes beyond the end of the sequence, a Eoferror exception is generated. When the file sequence is opened, the PIL library is automatically assigned to frame No. 0. Note: In the current version, most sequence formats allow the user to find only the next frame, and cannot jump-find the specified frame. Example: fromPILImportImageim_gif= Image.open ("Xin.gif") print (Im_gif.mode) Im_gif.seek (2) Im_gif.save ("He1.png") Im_gif.seek (8) Im_gif.save ("He2.png") Output:
'P'Note: The 2nd and 8th frames were found by code above.

26. Show

definition: im.show () Meaning: displays an image. This method is mainly used for debugging. On the UNIX platform, this method saves the image as a temporary PPM file and invokes the XV function. In widows, it saves the image as a temporary BMP file and displays it using the standard BMP display function. This method returns NULL. Example:fromimport  Imageim = Image.open ("Jing.png") Im.show ()

27. Split

definition: im.split ()? Sequence meaning: Returns a tuple that consists of each channel of the current image. For example, separating an "RGB" image will result in three new images, each corresponding to each channel of the original image (red, green, blue). Example:fromimport= Image.open ("jing.png"=  Im.split () b.save ("he.png")print(B.getpixel ((1,3)))

28. Tell

definition: Im.tell ()? Integer meaning: Returns the position of the current frame, calculated starting at 0. Example:fromimport= Image.open ("xin.gif")  Print(Im_gif.tell ()) Im_gif.seek (8)print(Im_gif.tell ()) output: 08

29, Thumbnail

  definition: im.thumbnail (size) im.thumbnail (size, filter) means: Modify the current image so that it contains a thumbnail of itself, The thumbnail size is not larger than the given size.     This method calculates an appropriate thumbnail size to match the aspect ratio of the current image, calls the method draft () Profile reader, and finally changes the size of the image. The variable filter should be one of nearest, BILINEAR, Bicubic, or AntiAlias. If the variable is omitted, the default is nearest. Note: In the current version of PIL, filters bilinear and bicubic do not fit well with thumbnail generation. Users should use AntiAlias for the best image quality.     If the processing speed is more important than the image quality, other filters can be selected. This method is modified on the original. If the user does not want to modify the original, you can use the method copy () to copy an image. This method returns NULL. Example:  from  PIL import   Imageim  = Image.open ("   Jing.png   " ) Im.thumbnail (( 10,10< Span style= "color: #000000")) Im.save (  "  He.png   " ) Note: Thumbnails have been created for image im01 that are not much more than 10x10. 

30, Tobitmap

Definition: Im.tobitmap ()? String meaning: Returns the bitmap image converted to X11.

31, Transform

define 1:im.transform (Size,method, data)? image Im.transform (size, method, data, filter)? Image meaning 1: Creates a new image with the given dimensions, with      The original image has the same pattern, using the given transformation method to copy the original data into the new images.      In the current version of PIL, the parameter method is extent (clipping a rectangular area), affine (affine transformation), QUAD (converting a square to a rectangle), MESH (one operation maps multiple squares), or perspective. The variable filter defines the filter for the pixels in the original image. In the current version, the variable filter is one of nearest, BILINEAR, Bicubic, or AntiAlias. If omitted, or the image mode is "1"or" P ", the variable is set to nearest.      Meaning 2: Crop an area from the image. The variable data is a 4-tuple (X0,Y0,X1,Y1) that specifies two coordinate points in the input image. The output image is the sampled result of pixels between these two coordinate points.      For example, if the input image (X0,Y0) is the (0,0) point of the output image, (x1,y1) is the same as the variable size. This method can be used to crop, enlarge, shrink, or mirror an arbitrary rectangle in the current image. It is slightly slower than method crop (), but as fast as the resize operation.      Meaning 3: Affine transformation of the current image, the transformation results are reflected in a given size of the new image. The variable data is a 6-tuple (A,B,C,D,E,F) that contains the first two lines of an affine transformation matrix. For each pixel in the output image (x, y), the new value is determined by the location of the input image (ax+by+c, dx+ey+f), using the closest pixel to approximate the pixel generation. This method is used to scale, transform, rotate, and crop the original image.
Define 4:im.transform (Size,quad, data)? Image im.transform (size, QUAD, data, filter)? Image meaning 4: A quadrilateral of the input image (defined by Four Corners) maps to a rectangle of a given size. The variable data is a 8-tuple (X0,Y0,X1,Y1,X2,Y2,X3,Y3), which includes the source quads, upper left, bottom left, lower right, and upper right Four corners. Meaning 5: Similar to quad, but variable data is a list of target rectangles and corresponding source quads. Define 6:im.transform (size,perspective, data)? Image Im.transform (size, perspective, data, filter)? Image meaning 6: A perspective transformation of the current image to produce a new image of a given size. The variable data is a 8-tuple (A,B,C,D,E,F,G,H) that includes the coefficients of a perspective transformation. For each pixel in the output image, the new value is from the location of the input image (a x+ b y + c)/(g x + H y + 1), (d x+ e y + f)/(g x + H y + 1) pixels,
Approximate with the closest pixel. This method is used for the 2D perspective of the original image.

32, Transpose

Definition: Im.transpose (method)? Image meaning: Returns a copy of the current image that is flipped or rotated. The value of the variable method is: flip_left_right,flip_top_bottom,rotate_90,rotate_180, or rotate_270. Example:fromimport  Imageim = Image.open ("Jing.png")im =  Im01.transpose (image.flip_left_right) Note: Image im mirrors the horizontal orientation of the image im01. 

33, Verify

definition: im.verify () Meaning: Try to determine whether the file is corrupt, and actually do not parse the image data. If this method finds any problems, it will produce the corresponding exception. This method only works on the image that was just opened, and if the image is already loaded, the result of the method will be undefined. If the user needs to load the image after using this method, the user needs to reopen the image file. Note: This method does not catch all errors; to catch a decoding error, the user must load the entire image. Example:fromimport  Imageim01 = Image.open ("Jing.png")  Im01.verify () Note: There is no output that indicates that the image im01 is undamaged. 

The second Python image processing module pil (pillow)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.