Image Module
Image modules within common in Python PIL image processing module. Such as open, save, conver, show ... and other functions
open (filename, mode) (an image open).
from PIL import Image
im = Inage.open("./static/gikki.jpg")
im.show()
im.show under way win win environment carrying the image display applications. Open and confirm the given image file. This is a lazy operation; the function will read the file header, and the real image data until it tries to process the data will be read from a file (call Load () method will be forced to load the image data). If the variable Mode is set, it must be an "r". Users can use a string (a string representing the file name), or as a file object of the file variable. File objects must implement the read (), seek () and tell () method, and the binary mode.
Save Class
from PIL import Image
im = image.open("./static/gikki.jpg")
print(im)
# 保存为png
im.save("./static/gikki.png")
print(im.format,im.size,im.mode)
Form class
from PIL import Image
im = image.open("./static/gikki.jpg")
print(im.format) # 打印出格式信息
im.show()
Mode class
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 pixels |
from PIL import Image
im = image.open("./static/gikki.jpg")
print(im.mode) ## 打印出模式信息
im.show()
e^{i\pi} + 1 = 0
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 & lt 299/1000 + G * + B * 114/1000 587/1000
when converted to two images (mode when "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 options 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()
size class
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()
Thumbnail class
Operating length and width of the image
from PIL import Image
im = Image.open("E:\mywife.jpg")
w, h = im.size
print('Original image size: %sx%s' % (w, h))
im.thumbnail((w//2, h//2))
im.save('thumbnail.jpg', 'jpeg')
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)
New class
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()
Copy
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
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 ().
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
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()
Draft
Loader configuration of the image file, so that the image returns a match with a given pattern and size of the possible versions.
from PIL import Image
im = Image.open("E:\mywife.jpg")
print(im.size,im.mode)
new_im = im.draft("L", (200,200))
print(new_im.size,new_im.mode)
new_im.show()
Rotate class
Back according to a given angle clockwise about the copy of the image after the image rotates. Variable filter is NEAREST, BILINEAR or one BICUBIC. If the variable is omitted or an image mode is "1" or "P", the default is NEAREST. The expand variable, if true, indicates that the output image is large enough, the rotated image may be loaded. If the default is false or, as large as the output image and the input image size.
from PIL import Image
im = Image.open("E:\mywife.jpg")
im_45 = im.rotate(45)
im_30 = im.rotate(30, Image.NEAREST,1)
print(im_45.size,im_30.size)
im_45.show()
im_30.show()