In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use Python to post a high-pressure circle of friends. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Today, Xiao Mo is going to introduce you to a Python library:
Let's use a practical example.
See what amazing things 50 lines of python code can do.
This is a moments of Xiaomofa.
Before cutting, there is a picture.
After cutting the picture, there are nine pictures.
Successfully dominate the screen
In addition to being able to handle regular square pictures
You can also handle irregular pictures.
For example, the following picture is much wider than the height.
How do you deal with er Pang?
It's simple
Take a larger value between width and height
Then fill it with white.
You can construct a square picture.
Of course, it's easy to make a picture like this, and now a lot of image processing software on mobile phones can do it.
Xiao Mo gives this example just to introduce you to the PIL library. Interested children's shoes can also practice programming.
All right, now let's take a look at what this PIL library is.
PIL is a very powerful Python image processing standard library, but because PIL supports Python2.7, programmers who use Python3 separate a branch from PIL and create another library, Pillow, which can support Python3.
Pillow is compatible with most of the syntax of PIL and is very easy to use.
Xiao Mo will talk about how to use the PIL library to implement Mini Program introduced above.
In fact, the idea is very simple:
The corresponding code is as follows:
#-*-coding: utf-8-*-'fill an image into a square and cut into 9 pictures Author: Wechat official account: big data frontier' 'from PIL import Image import sys # fill the picture as a square def fill_image (image): width Height = image.size # choose the larger values in length and width as the new picture's new_image_length = width if width > height else height # generate a new picture [white background] new_image = Image.new (image.mode, (new_image_length, new_image_length), color='white') # paste the previous picture on the new picture Center if width > height:# the width of the original picture is greater than the height Then the vertical dimension # (xPowery) tuple of the filled image represents the starting position of the pasted image above relative to the following image 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 image def cut_image (image): width Height = image.size item_width = int (width / 3) box_list = [] # (left, upper, right, lower) for i in range (0Jing 3): # double loop Generate 9 pictures based on the location of the original image for j in range (0P3): # print ((itemic 1) * item_width, (juni1) * item_width)) box = (jacked itemic widthbased on the location of the original image, (job1) * item_width (iTun1) * item_width) box_list.append (box) image_list = [image.crop (box) for box in box_list] return image_list # Save def save_images (image_list): index = 1 for image in image_list: image.save ('. / result/python'+str (index) + '.png' 'PNG') index + = 1 if _ 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 you read the code to know how to achieve it, the code may be more difficult to understand the place Xiaomo also gave the corresponding comments, we quickly download the code to take a run.
The above is the editor for you to share how to use Python to post a high-pressure circle of friends, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.