PIL module in python

Image Module

Image module are common in Python PIL image processing module based on the image operation functions are substantially incorporated within the module. Such as open, save, conver, show ... and other functions.

open class

Image.open(file) ⇒ image 
Image.open(file, mode) ⇒ image

Load an image from a file, use the open () function in the Image module:

from PIL import Image             ##调用库
im = Image.open("E:\mywife.jpg")  ##文件存在的路径
im.show()                         

Save Class

im.save(outfile,options…) 
im.save(outfile, format, options…)

To save the file, save Image class is used () method, then save the file name becomes very important, unless the specified format, or the name of the library will be saved as a file name extension format. Use the given file name to save the image. If the variable is the default format, if possible, from the format of the file extension to determine the file name. This method returns null. Keyword options provide additional instructions to the file writer. If the writer does not recognize an option, it will ignore it. Users can use the file object instead of the file name. In this case, the user must specify the file format. File objects must implement seek (), tell () and write () method, and it opened in binary mode. If the method save () fails for some reason, this method generates an exception (usually IOError exception). If an exception occurs, the method also might have created a file, and write some data files. If desired, the user application can delete the incomplete file.

from PIL import Image

im = Image.open("D:\mywife.jpg")
print(im)
im.save("D:\mywife.png")   # 将"E:\mywife.jpg"保存为"D:\mywife.png"
im = Image.open("D:\mywife.png")  ##打开新的png图片
print(im.format, im.size, im.mode)

format class

This attribute identifies the source image, if the image is not read from the file its value is None.

from PIL import Image
im = Image.open("E:\mywife.jpg")
print(im.format) ## 打印出格式信息
im.show()

Mode class

Mode image, there is a common mode "L" (luminance) represented by gray scale image, "RGB" indicates true color images, and "CMYK" represents Publishing image indicating pixel format images used. The following table is a common description of nodes:

modes description
1 A pixel, black and white, into 8-bit pixel memory
L 8 pixels, black and white
P 8-bit pixel, palette mapping to use any other pattern
RGB 3 × 8-bit pixels, true color
RGBA 4 × 8-bit pixels, true color-plus-clear channel
CMYK 4 × 8-bit pixels, the color isolation
YCbCr 3 × 8-bit pixels, color video format
I 32-bit integer pixels
F 32-bit floating-point pixel

from PIL import Image
im = Image.open("E:\mywife.jpg")
print(im.mode)   ## 打印出模式信息
im.show()

convert Class

The current image is converted to another mode, and returns a new image. When converting from a palette image, pixel by this method to convert this palette. If the mode variable is not assigned, the method will select a mode, in the absence of the palette, the palette so that the image is all the information and can be represented. When the color image into a monochrome image, PIL library ITU-R601-2 luma conversion formula:

L = R * 299/1000 + G * 587/1000 + B * 114/1000
When the image is converted to 2 (mode "1"), the source image is first converted into a monochrome image. Result data values ​​greater than 127 are set to white, the other is set to black; this image will jitter. If another threshold value, change the threshold 127, the method may be used point (). In order to remove the image jitter, dither option can be used.

from PIL import Image
im = Image.open("E:\mywife.jpg")
new_im = im.convert('P')
print(new_im.mode)
new_im.show()

im.convert(“P”,**options) ⇒ image
This is defined as the first method, but when the "RGB" image is converted to 8-bit palette image can better handle. The options are:

Dither =. Dithering control. The default is FLOYDSTEINBERG, errors committed together with the adjacent pixels. This feature is not enabled, then assigned to NONE.

Palette =. Palette generation control. The default is the WEB, which is the standard 216-color "web palette". To optimize the use of the palette, then assigned ADAPTIVE.

Colors =. When the palette option is ADAPTIVE, for controlling the number of color palette. The default is the maximum value, i.e., 256 colors
im.convert(mode,matrix) ⇒ image
using a conversion matrix "RGB" image into an "L" or "RGB" image. 4 is a variable matrix or 16-tuple.


from PIL import Image
im = Image.open("E:\mywife.jpg")
print(im.mode)
rgb2xyz = (0.412453,0.357580, 0.180423, 0,
           0.212671,0.715160, 0.072169, 0,
           0.019334,0.119193, 0.950227, 0 )
new_im = im.convert("L", rgb2xyz)
print(new_im.mode)
new_im.show()

Size class

im.size ⇒ (width, height)
Size of the image is calculated according to the number of pixels, its return value tuple width and height (width, height).

from PIL import Image
im = Image.open("E:\mywife.jpg")
print(im.size) ## 打印出尺寸信息
im.show()

Palette category

Color palette table. If the image mode is "P", ImagePalette class instance is returned; otherwise, will be None.
For the palette is shown below in a non-image information "P" mode.

from PIL import Image
im = Image.open("E:\mywife.jpg")
print(im.palette)

Convert the image operation, converted into the "P" mode

from PIL import Image
im = Image.open("E:\mywife.jpg")
new_im = im.convert('P')
print(new_im.mode)
print(new_im.palette)

Info class

Dictionary storing image-related data. Using the dictionary file handle passed various non-image information read from the file. Most methods return when the new image will ignore this dictionary; key because the dictionary is not standardized, for a method that does not know how their operations affect the dictionary. If you need this information, you require () to save the dictionary in return method open.

from PIL import Image
im = Image.open("E:\mywife.jpg")
print(im.info)

new class

Image.new(mode,size) ⇒ image 
Image.new(mode, size,color) ⇒ image

Given variable size and mode to generate a new image. Size is given width / height tuple, which is calculated according to the number of pixels. For single-channel image, only given a variable color value; For multi-channel images, given a variable color tuple (a value per channel). In version 1.1.4 and later, users can also use the name of the color, such as color assigned to the variable "red". If there is no color assignment to a variable, the entire image content will be assigned to 0 (black). If the variable color is empty, images will not be initialized, that is, the content of the image of all zeros. This is useful to copy images or draw something.

As to set the image size is 128x128 red image.

from PIL import Image
im = Image.open("E:\mywife.jpg")
n_im= Image.new("RGB", (128, 128), "#FF0000")
n_im.show()

The image size is 128x128 as the black image, because the variable is not assigned, then color, the image content is set to 0, i.e. black.

from PIL import Image
im = Image.open("E:\mywife.jpg")
n_im= Image.new("RGB", (128, 128))
n_im.show()

Copy category

Copy the image. If you want to paste some data to this chart, you can use this method, but the original image will not be affected.


from PIL import Image
im = Image.open("E:\mywife.jpg")
im_copy = im.copy()

Crop class

im.crop(box) ⇒ image
It returns a copy of the rectangular area from the current image. Variable is a four-tuple box, define the left, on the right and at the pixel coordinates. Taken to represent position coordinates in the original image, such as a box (100,100,200,200) on the original image shown in the upper left corner as the origin of coordinates, the interception of a 100 * 100 (pixels) of an image for ease of understanding, the following is a schematic box (b1, a1, b2, a2). Mapping software Visio2016. This is a lazy operation. Changes to the source image may or may not be reflected in the image cut down. To obtain a copy of a separate copy of the call to the cutting load ().

@zhangziju
from PIL import Image
im = Image.open("E:\mywife.jpg")
box = (300, 100, 700, 700)              ##确定拷贝区域大小
region = im.crop(box)                   ##将im表示的图片对象拷贝到region中,大小为box
region.show()

Paste class

im.paste(image,box)
FIG adhered to the one on top of another image. Variables given a box or a 2-tuple of the upper left corner, or define the left, upper, right and lower 4-tuple pixel coordinates, or is empty (the (0,0) the same). If the 4-tuple given, the size of an image to be pasted with the same area size. If the pattern does not match, the pasted image is converted into a current-mode image.

from PIL import Image
im = Image.open("E:\mywife.jpg")
box=[0,0,100,100]
im_crop = im.crop(box)
print(im_crop.size,im_crop.mode)
im.paste(im_crop, (100,100))             ##(100,100,0,0)
im.paste(im_crop, (400,400,500,500))
im.show()

Filter class

im.filter(filter) ⇒ image

Returns a copy of the given filter processing through use of the image. In particular reference to the application ImageFilter image filtering module in the module, a number of predefined enhancement filter can be used by a filter function (), the predefined filter comprising: BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES , SMOOTH, SMOOTH_MORE, SHARPEN. Wherein the mean filter is a BLUR, CONTOUR find contour, FIND_EDGES edge detection, when using the module, the need to import.

from PIL import Image
from PIL import ImageFilter                         ## 调取ImageFilter
imgF = Image.open("E:\mywife.jpg")
bluF = imgF.filter(ImageFilter.BLUR)                ##均值滤波
conF = imgF.filter(ImageFilter.CONTOUR)             ##找轮廓
edgeF = imgF.filter(ImageFilter.FIND_EDGES)         ##边缘检测
imgF.show()
bluF.show()
conF.show()
edgeF.show()

Reference links

Guess you like

Origin www.cnblogs.com/tomyyyyy/p/11122814.html