Python image 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. 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).

    name RGBA values name RGBA values
    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 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()

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-PNlCJSPz-1581377786709) (./ res / image-show.png)]

  1. Crop the image

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

    [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-XaZVivu7-1581377786711) (./ res / image-crop.png)]

  2. Generate thumbnails

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

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

    [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-dnQtD7fV-1581377786713) (./ res / image-paste.png)]

  2. Rotate and flip

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

    [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-NKCptQKC-1581377786713) (./ res / image-rotate.png)]

    [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-GQgjrv46-1581377786714) (./ res / image-transpose.png)]

  3. Operation pixels

    >>> 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()
    

    [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-WJKInyXz-1581377786714) (./ res / image-putpixel.png)]

  4. Filter effects

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

    [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-GISuXQJP-1581377786715) (./ res / image-filter.png)]

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 manual and use the document to view its official documents .

Word processing documents

Use 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 word processing software are free.

Processing 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.


I welcome the attention of the public number, reply keyword " Python ", there will be a gift both hands! ! ! I wish you a successful interview ! ! !

Published 95 original articles · won praise 0 · Views 3061

Guess you like

Origin blog.csdn.net/weixin_41818794/article/details/104257963