In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to generate nine-grid photos of Python", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to generate nine-grid photos of Python" article.
First, the effect picture of the ninth palace in the moments. 2. The basic operation of the picture.
Open the picture to be processed
Judge whether the open picture is square or not
If it is a square, divide it into nine equal parts. If it is not a square, fill it with white first, and then divide it into nine equal parts.
Save the processed picture
The first step is to install the Pillow module.
Pip install pillow
1. Deal with pictures
In Pillow, use the Image submodule. The Image.open function returns an image object with the following code:
From PIL import Image# reads the picture img = Image.open ('lbxx.jpg')
The Pillow module is the python3 version of the PIL module, so we use the following statement when we import the module:
From PIL import Image
2. Intercept pictures
In the Image object, there is a crop method that cuts the picture. It receives a box parameter that represents the area to be intercepted. The coordinate system in the picture is based on the upper left corner.
Let's look at the specific code:
From PIL import Image# reads the picture img = Image.open ('lbxx.jpg') # intercepts the (0,0,300,300) area of the picture box = img.crop ((0,0,300,300)) # shows the intercepted area box.show ()
3. Save the processed pictures
Saving the image is very simple. All we need to do is call the save method of img, as shown in the following code:
From PIL import Imageimg = Image.open ('lbxx.jpg') box = img.crop ((0,0,300,300)) # Save picture box.save (' 1.jpg') 3. Generate Nine Miyagi grid pictures
Knowing the above operation, the following operation is nothing more than to intercept the nine areas of the picture and save it. The specific code is as follows:
Import osfrom PIL import Image# reads picture im = Image.open ('1kkx.jpg') # divide width and height by 3 Get the size of the cropped single picture width = im.size [0] / / 3height = im.size [1] / / 3 # coordinate of the upper left corner of the cropped picture start_x = 0start_y = 0 # used to name the picture im_name = 1 # cycle cropping picture for i in range (3): for j in range (3): # crop the picture and save crop = im.crop ((start_x, start_y, start_x+width) Start_y+height)) # determine whether if not os.path.exists ('imgs'): os.mkdir (' imgs') crop.save ('imgs/' + str (im_name) +' .jpg') # move the x axis of the upper left corner coordinates to the right start_x + = width im_name + = 1 # when first After the line is cut, x continues to crop from 0. Start_x = 0 # cut the second line start_y + = height above is the content of this article on "how Python generates Nine Palace Photos". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to 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.