Send a high-fashion friend in Python - a simple application of image processing with code


python is an open source scripting language with a very large number of open source libraries that enable many unexpected little features

Today I would like to introduce you to a Python library.

PIL(Python Image Library)

Here we use a practical example

See what amazing things 50 lines of python code can do

This is a friend I posted

Before the cut is a picture

After the cut, there are nine pictures.

Successful Screen Domination

In addition to working with regular square images

Can also handle non-regular images

For example, the following image is much wider than it is tall

How was it handled?

It's simple.

Take the larger value between the width and height

Then fill with white

Then you can construct a square picture.

Of course, it's easy to make a picture look like this, as many photo manipulation apps on your phone can do it now.

This example is just to introduce you to the PIL library, interested children can also practice programming practice ~

Well, now let's see what this PIL library really is~

PIL is a very powerful python image processing standard library, but then, since PIL supports Python 2.7, programmers using Python 3 have separated out a branch of PIL and created another library, Pillow, that is Python 3 capable.

Pillow is compatible with most of the syntax of PIL and is very easy to use.

Here's how Two Fatty used the PIL library to implement the applet described above.

The idea is actually quite simple.

The corresponding codes are as follows.

# -*- coding: utf-8 -*-'''Fill an image with a square and then cut it to9 chartAuthor: WeChat Public: Big Data Frontier'''from PIL import Imageimport sys# Fill the image with a squaredef fill_image(image): width, height = image.size # Select the larger of the length and width as the new image's new_image_length = width if width > height else height # Generate a new image[ white background] new_image = Image. new(image.mode, (new_image_length, new_image_length), color='white') # Paste the previous image on the new one, centering if width > height:# The original image is wider than it is tall, then the vertical dimension of the filled image#(x,y) The binary represents the starting position of pasting the top figure relative to the bottom figure new_image.paste(image, (0, int((new_image_length - height) / 2))) else: new_image.paste(image, (int((new_image_length - width) / 2),0)) return new_image# cut a mapdef cut_image(image): width, height = image.size item_width = int(width / 3) box_list = [] # (left, upper, right, lower) for i in range(0,3):# twofold cycle, generate9 The position of the image based on the original image for j in range(0,3): #print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width)) box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width) box_list.append(box) image_list = [image.crop(box) for box in box_list] return image_list# savedef save_images(image_list): index = 1 for image in image_list: image.save('./result/python'+str(index) + '.png', 'PNG') index += 1if __name__ == '__main__': file_path = "python.jpeg" image = Image. open(file_path) #image.show() image = fill_image(image) image_list = cut_image(image) save_images(image_list)

I believe that you know how to implement the code, the code may be difficult to understand the corresponding comments are also given, you quickly download the code to take a run.


Recommended>>
1、Qiannan delegation participated in the Qianxing Shenzhen Thousands of miles to find Miao Matchmaking Conference
2、Cat Chain Weekly NewsNo29
3、Little snapper python course study notes similarities and differences between lists tuples and strings
4、Artificial intelligence machine learning desperately needs data and the more data there is the more accurate the results will be
5、Liaocheng Special Session and Cybersecurity Forum of the Lake Minghu Talk series of public welfare activities was successfully held

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号