Python uses PIL to cut and stitch pictures

The examples in this article share the specific code for cutting pictures and stitching pictures using PIL in python for your reference. The specific content is as follows

Due to work needs, I came into contact with the PIL package, and saw other people’s blogs stepped on some pits. Some blogs did not indicate the meaning of each position parameter. Today I will fill it in.

crop

  1. First download a picture, I use a 1200*1200 pixel picture, and place it in the img directory of the G drive

  1. I cut this picture into quarters, I created two new files, img1 and img2, to store the pictures

  1. Code
from PIL import Image
img = Image.open('G:/img/1.jpg')print('The size of the picture is:{}'.format(img.size))print('Divide the picture into four equal parts, 4 pictures of 600 X 600')
size =600 #The image size is 600, so only one variable is set
left =0 #The product value of the width of the image from the left
shang =0 #The product of the width of the image from the top
index =0 #Picture name
for i inrange(4):if i ==2:
 # When looping to the third value, you need to cut the second row of pictures
 shang +=1
 left =0
 a = size * left #The size of the picture from the left
 b = size * shang #The size of the picture from the top
 c = size *(left +1) #The size of the picture from the left+Image width
 d = size *(shang +1) #The size of the picture from the top+Picture height
 print('a= {},b= {},c= {}, d= {}'.format(a,b,c,d))
 croping = img.crop((a,b,c,d))
 croping.save('G:/img/img1/'+str(index)+'.jpg')
 index +=1
 left +=1

Look at the running results:

Take a look at the file, and save this picture into the img1 directory after segmentation

Click to open the catalog to see, it is the effect we want

Two, splicing

  1. Splicing on the basis of the divided pictures, save the pictures into the img2 folder

Code:

from PIL import Image
 
imgname =0
def pingjie(imgs):print('------------pingjie-------------')
 target = Image.new('RGB',(size *2, size *1)) #Before stitching, you need to write the picture size after stitching is 1200*600for i inrange(len(imgs)):
 a = size * i  #The size of the picture from the left
 b =0  #The size of the picture from the top
 c = size *(i +1) #The size of the picture from the left+Image width
 d = size  #The size of the picture from the top+Picture height
 target.paste(imgs[i],(a, b, c, d))
 global imgname
 print('The path for stitching pictures is:',path1 +str(imgname)+'.jpg')
 target.save(path1 +str(imgname)+'.jpg')
 imgname +=1
 
def pj():print('------------pj-------------')
 # Take 1,3 is because each row is the last one when stitched together, the first row is 0, 1 is named, and the second row is 2,3 named, so take the last value
 imglist =[1,3]
 img =[]for i in imglist:print('The splicing path of the complete line is:'+ path1 +str(i)+'.jpg')
 img.append(Image.open(path1 +str(i)+'.jpg'))
 target = Image.new('RGB',(size *2, size *2)) #Before stitching, you need to write the picture size after stitching is 1200*1200for i inrange(len(img)):
 a =0 #The size of the picture from the left
 b = size * i #The size of the picture from the top
 c = size *2 #The size of the picture from the left+Image width
 d = size *(i +1) #The size of the picture from the top+Picture height
 target.paste(img[i],(a, b, c, d))
 global imgname
 target.save(path1 +'pingjie'+'.jpg')if __name__ =='__main__':
 size =600 #The picture’s width and height are both 600 pixels
 path ='G:/img/img1/' #Directory to store pictures to be stitched
 path1 ='G:/img/img2/' #Storage directory of pictures after stitching
 index =0 #Picture name
 for i inrange(2): #There are two rows, so you need to loop twice
 images =[] #Each splicing can only be spliced one line by one line, and the second line of pictures cannot be spliced after the first line is spliced. The matrix does not allow this operation
 for j inrange(2): #There are two pictures in each row, so we have to loop twice
 print(path +str(index)+'.jpg')
 images.append(Image.open(path +str(index)+'.jpg'))
 index +=1print('First{}Line stitching completed'.format(i))pingjie(images)pj()

Look at the print result:

Look at the following directory: see pictures stored in img2

Enter the directory: you can see that the splicing is successful

This article has been included in the topic "Python Image Processing Operations", welcome to click to learn more exciting content.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python uses PIL to cut and stitch pictures
Magic methods and uses of Python
Python uses pdfkit to generate pdf [python]
How to use and and or in Python
Use Python to quickly cut out images
Python uses pandas to process Excel data
Python realizes horizontal and vertical splicing of pictures
Python uses the email module to send mail
How to delete files and directories in python
How to read and write files with Python
Python and Go
Introduction to Python
Python reads the result and writes it to Excel
Example of how to automatically download pictures in python
Python uses the re module to verify dangerous characters