python image and office document processing summary

Program to process images and office documents often appear in the actual development, although the Python standard library module does not directly support these operations, but we can perform these operations by Python ecosystem of third-party modules.

Operation image

Computer graphics-related knowledge

colour. If you have experience using pigment to paint, then we know that mixing red, yellow, and blue pigments can be other colors, three colors is the fact that what we call the three primary colors of art, they are no longer decomposed basic color. In the computer, we can be red, green, and blue colored light in various proportions to assemble the overlay other color, the three colors is three primary color lights, we will usually represented as a color value or a RGB RGBA values ​​(wherein a represents the Alpha channel, it is determined through this image pixel, i.e. transparency).

'''
遇到python不懂的问题,可以加Python学习交流群:1004391443一起学习交流,群文件还有零基础入门的学习资料
'''

Pixels. For an image represented by the digital sequence, the smallest unit is a single color image of the small box, the small blocks has a specific location and color values ​​are assigned, and the color of the position of a small square, and determine the final presentation of the image out of the way, they are indivisible unit, we usually called pixels (pixel). Each image contains a certain amount of pixels that determine the size of the image on the screen presented.

Pillow operation image with

Pillow is a branch developed from the well-known image processing library Python PIL, various operations such as image compression and image processing can be achieved by Pillow. You can use the following command to install Pillow.

pip install pillow

Pillow is the most important thing is the Image class, read and process the image should be done by this class.

>>> from PIL import Image
>>>
>>> image = Image.open('./res/guido.jpg')
>>> image.format, image.size, image.mode
('JPEG', (500, 750), 'RGB')
>>> image.show()

Crop the image

>>> image = Image.open('./res/guido.jpg')
>>> rect = 80, 20, 310, 360
>>> image.crop(rect).show()

 

Generate thumbnails

>>> image = Image.open('./res/guido.jpg')
>>> size = 128, 128
>>> image.thumbnail(size)
>>> image.show()

 

Zoom and paste images

>>> image1 = Image.open('./res/luohao.png')
>>> image2 = Image.open('./res/guido.jpg')
>>> rect = 80, 20, 310, 360
>>> guido_head = image2.crop(rect)
>>> width, height = guido_head.size
>>> image1.paste(guido_head.resize((int(width / 1.5), int(height / 1.5))), (172, 40))

 

Guess you like

Origin blog.csdn.net/qq_40925239/article/details/92702555