Python100Days study notes --- Day15 images and office document processing

Image and document processing office

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
1, color. 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).

名称	RGBA值	名称	RGBA值
White	(255, 255, 255, 255)	Red	(255, 0, 0, 255)
Green	(0, 255, 0, 255)	Blue	(0, 0, 255, 255)
Gray	(128, 128, 128, 255)	Yellow	(255, 255, 0, 255)
Black	(0, 0, 0, 255)	Purple	(128, 0, 128, 255)

2, 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 may be implemented image compression and image processing 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()

Here Insert Picture Description

1, crop the image

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

Here Insert Picture Description
2, generate thumbnails

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

Here Insert Picture Description
3, scaling and pasting 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))

Here Insert Picture Description
4, rotating and flipping

>>> image = Image.open('./res/guido.png')
>>> image.rotate(180).show()
>>> image.transpose(Image.FLIP_LEFT_RIGHT).show()

Here Insert Picture Description

Here Insert Picture Description

5, operation of the pixel

>>> image = Image.open('./res/guido.jpg')
>>> for x in range(80, 310):
...     for y in range(20, 360):
...         image.putpixel((x, y), (128, 128, 128))
... 
>>> image.show()

Here Insert Picture Description

6, filter effects

>>> from PIL import Image, ImageFilter
>>>
>>> image = Image.open('./res/guido.jpg')
>>> image.filter(ImageFilter.CONTOUR).show()

Here Insert Picture Description
Excel spreadsheet processing

Python's openpyxl module allows us to read and modify Excel spreadsheet program in Python, of course, the actual work, we could be handled Excel spreadsheet file with LibreOffice Calc and OpenOffice Calc, which means that the module can handle openpyxl software generated from these spreadsheet. About openpyxl manuals and documentation can view it using official documents

Word processing documents
using the python-docx module, Pytho can create and modify Word documents, of course, here not only refers to the Word document file extension created by Microsoft's Office software, called the docx, LibreOffice Writer and OpenOffice Writer is a free word processing software

Handling PDF documents
PDF is the acronym for Portable Document Format, the use of .pdf as the file extension. Next we will look at how to read text from PDF and generate a new PDF file from an existing document by Python.

Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/105207460