An introduction to the image module
Create a new picture Python code image.new (mode, size) image.new (mode, size, color)
Image.new (mode, size)
image.new (mode, size, color)
Cascade Pictures
Cascade two pictures, Img2 and Img2,alpha is a floating-point number between [0,1], if 0, the effect is IMG1, if 1.0, the effect is img2. Of course IMG1 and IMG2 must be the same size and pattern. This function can make a beautiful effect, and the arithmetic plus and minus of the graph will be said. Python code image.blend (IMG1, IMG2, Alpha)
Image.blend (IMG1, IMG2, Alpha)
Composite can use another picture as a mask (mask), all of these three pictures must have the same size, the mask pattern can be "1", "L", "RGBA" (see previous for mode) Python code image.composite ( IMG1, IMG2, mask)
Image.composite (IMG1, IMG2, mask)
Convert graphics mode
Here is a comparison of the cattle method convert, this method can transform the picture between different modes, when the grayscale map to two-value graph, all the non-0 values are set to 255 (white). Grayscale maps are converted using this algorithm: Write L = r*299/1000 + g*587/1000 + b*114/1000
(This is the itu-r 610-2 brightness conversion equation)
Point operation
Img.point (function), which takes a parameter and executes the function on every point in the picture, which is an anonymous function, which can be used in functional programming languages such as Python.
Lambda expressions to complete, such as python code out = Img.point (lambda i:i*1.2) #对每个点进行20% of the reinforcement
out = Img.point (lambda i:i*1.2) #对每个点进行20% of the reinforcement
If the picture is "I" or "F" mode, then this lambda must use this form
Python code argument * scale + offset e.g out = img.point (lambda i:i*1.2 + 10)
Argument * scale + offset
e.g out
= img.point (lambda i:i*1.2 + 10)
transparent channel usage
Putalpha (Alpha)
This is a fantastic way to write a picture (the same size as the original) into the transparent channel of the image. Without affecting the normal display of the original picture, can be used for information hiding oh. Of course, the premise is that the original
image has a transparent channel. But even if it is not, there is no relationship, because there are PIL to provide the conversion function, you can convert a picture into RGBA mode, and then the information file to be hidden into the "L" or "1" mode
, and finally use this putalpha to overlay it. And in the picture of the user, simply need to extract the transparent channel which can be seen hidden information, haha.
Python code def hideinfoinimage (img, info): if img.mode != "RGBA": img = Img.convert ("RGBA") if info.mode != "L" and info.mode != "1": info = Info.convert ("L") img.putalpha (info) return img
Def hideinfoinimage (IMG, info):
if Img.mode!= "RGBA":
img = Img.convert ("RGBA")
if Info.mode!= "L" and info. Mode!= "1":
info = Info.convert ("L")
Img.putalpha (info) return
img
Test python code if __name__ = = "__main__": img = Image.open ("green.png") band = Image.open ("ANTELOPE_INHALF.J PG ") img = Hideinfoinimage (img, band) img.show () #可以看到, the original picture does not have an explicit change img.split () [3].show () #抽取出透明通道中的图片并 Show
if __name__ = = "__main__":
img = Image.open ("Green.png")
band = Image.open ("antelope_inhalf.jpg")
img = Hideinfoinimage (IMG, band)
Img.show () #可以看到, the original picture does not have an explicit change
img.split () [3].show () #抽取出透明通道中的图片并显示
In Windows systems, the default picture browser attaches transparent channels, may affect the effect, can be viewed in a different picture browser.
Python Code def randompalette (length, Min, max): return [Randint (min, max) for x in xrange (length)]
def randompalette (length, Min, max): Return
[Randint (Min, max) for x in xrange (length)]
Use of Palette Python Code putpalette (palette) img = Image.open ("Green.png"). Convert ("L") #img. Show () L = Randompalet Te (768, 0, 255) Img.putpalette (L) img.show ()
Putpalette (palette)
img = Image.open ("Green.png"). Convert ("L")
#img. Show ()
L = Randompalette (768, 0, 255 )
Img.putpalette (L)
img.show ()
Now convert Green.png to grayscale, and then customize a random palette coloring to see this effect. Maybe someone is thinking about the role of this graphic processing, in fact, the role is relatively large, do pattern recognition, human face detection
Measurement and so on operations, true color pictures are often difficult to deal with, a solution is to deal with grayscale or binary graphs, one can remove some of the interference, and secondly can reduce the amount of data to be processed (improve efficiency).
Picture conversion (distorted, mapped)
The transform () method uses Python code transform (size, method, data)
Transform (size, method, data)
Method parameter:
EXTENT |
Cut a rectangle out (to cut, stretch, compress, etc.) |
Affine |
Geometry anti-shooting conversion |
QUAD |
Maps a quadrilateral to a rectangle |
MESH |
Mapping multiple quadrilateral to an action |
In the case of extent parameters, data is a four-tuple (x0,y0,x1,y1) that represents the area to be processed in the input image, the size of the new picture, and a two-tuple (width, height)
Use: clip, stretch, compress, etc.
In the case of the affine parameter, data is a six-tuple (a,b,c, d,e,f) that contains the first two lines of the affine transpose matrix. For each pixel (x,y) of the output picture, it is replaced with the new value (Ax+by+c, dx+ey+f).
Use: inversion, rotation, shearing, etc.
In the case of the Quad parameter, data is a eight-tuple (x0,y0,x1,y1,x2,y2,x3,y3) representing the coordinate value of the four fixed-point points of the source quadrilateral. Mesh way with quad effect is quite, but to multiple quadrilateral mapping operation.
Transpose Python code Im.transpose (method)
Im.transpose (method)
Method parameter:
Flip_left_right |
Invert left and right |
Flip_top_bottom |
Upside Down |
Rotate_90 |
Rotate 90 degrees (counterclockwise) |
rotate_180 |
Rotate 180 degrees (counterclockwise) |
rotate_270 |
Rotate 270 degrees (counterclockwise) |
(360 degrees? This is not a turn, hehe)
About the image module is introduced here, there are some attributes, functions and methods do not do this analysis, this part is relatively simple, if you want to know more, you can see PiL's Handbook.