How to use python Cut the picture
One 、 install PIL library
Two 、 Construction ideas
3、 ... and 、 Complete code
summary
How to use python Cut the picture As shown in the figure above , This is a picture containing all kinds of chess pieces . We need to cut out every piece , You can use python Of PIL library
Realization .
If not previously installed PIL library , stay python3 In the environment , Command line input pip install pillow
Installation is complete .
We Try it , Put the black in the upper left corner take Cut it out and save the picture .
First, open the drawing , Zoom in and out , Then check the grid line , Find the... That will this piece Top left coordinates and The top right coordinates . You can see that the coordinates in the lower right corner are about (50,50). The upper left corner is (0,0). Because the initial point of the coordinates of all pictures is in the upper left corner .
from PIL import Imageimg = Image.open('chess.png') ## open chess.png file , And assign it to imgregion = img.crop((0,0,50,50))## 0,0 The upper left coordinate of the position to be cropped ,50,50 Indicates the lower right corner .region.save(' give an example .png') ## Save the cropped picture to give an example .png
It can be seen that the cutting is successful , It should be noted that , We have to cut out every piece , Do you want to look at the coordinates one by one 、 Modify the coordinates in the program , Then run the program over and over again to crop the picture ? It's better to use drawing and cutting directly, which may be faster . Let's think about it again , Will find The space between each piece is regular , Can we try to use for Loop to realize batch cutting ?
3、 ... and 、 Complete codeOpen the drawing again , You can find the law . The coordinates of the upper left corner of the first row of black children are (0,0),x Coordinates are added every time 50, Plus seven times , So it is (0,0)-(300,0); In the lower right corner, you can get (50,0)-(350,0); The second row is (0,50)-(300,50) and (50,100)-(350,100). After understanding the law , Just start typing code
from PIL import Imagechessred = ['jj','ss','xx','mm','cc','pp','bb'] ## Red chess is named , It's casual , Just don't be the same chessblack = ['j','s','x','m','c','p','b'] ## Black chess is named , It's casual , Just don't be the same img = Image.open('./chess.png') ## open chess.png, And assign it to imgdef cropimg(): for i,j,z in zip(range(0,350,50),chessblack,chessred): region = img.crop((i,0,i+50,50))## Cut the first row of black chess pieces region.save(f'./test/{j}.png')## Save the first row of black chess pieces region = img.crop((i,50,i+50,100))## Cut the second row of red chess pieces region.save(f'./test/{z}.png')## Save the second row of red chess pieces cropimg()
Open folder , All cropped images have been saved .
summaryThis is about using python This is the end of the article on clipping pictures , More about python Please search the previous articles of the software development network or continue to browse the relevant articles below. I hope you can support the software development network in the future !