The python implementation quickly divides multiple images into two -- or even more and rotated
Because recently, due to the need of learning (entertainment), the author needs to divide a large number of pictures into two from the middle. I searched all over and couldn't find such a method or program, so I had to apply what I learned and write one in python.
It mainly uses the pilot library and os library. A reference link will be attached at the end of the article
Because I can't write a function (actually it can't run...) I have to write it twice to achieve the corresponding goal
It should be noted that the folder to be cut must not contain file formats other than pictures, otherwise an error will be reported when reading (it should be filtered by writing a branch)
The set coordinates (box) used to intercept the picture are the upper left corner and the lower right corner of the part to be intercepted respectively; The coordinates of the upper left corner of the picture are (0,0), and the coordinates of the lower right corner are the maximum value of length and width
Due to file reasons, I won't put my result preview here
1. Traverse all the pictures in a folder and divide them into two
from PIL import Image import os path = 'X:\\file\\New folder' #File directory #The path directory needs to be changed manually after processing path_list = os.listdir(path) #path_list.remove('.DS_Store') #The file management file in macos is hidden by default. It can be ignored here. If it is a mac, this line may need to be added back (the author has not used mac) print(path_list) for i in path_list: #Cut the left half of the picture a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (0,0,w*0.5,h) #The box tuple is the coordinates of the upper left corner and the lower right corner of the part to be intercepted in the processed picture img = img.crop(box) print('Intercepting the left half...') img.save('L'+i) #Here, it is necessary to add a letter to the cut figure for identification to prevent coverage due to the same name print('L-',i,'Saved successfully') for i in path_list: #Intercept the right half of the picture a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.5,0,w,h) img = img.crop(box) print('Intercepting the right half...') img.save('R'+i) print('R-',i,'Saved successfully') print("'{}'All pictures in the directory have been saved to the directory of this file.".format(path))
2. Traverse all the pictures in a folder and divide them into ten
Similarly, when you need to intercept ten parts of the picture, you can increase the number of runs and change the parameters:
from PIL import Image import os path = 'X:\\file\\Jigsaw puzzle a4' #File directory #The path directory needs to be changed manually after it is truncated path_list = os.listdir(path) print(path_list) for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (0,0,w*0.1,h) img = img.crop(box) print('Intercepting the first part...') img.save('A'+i) print('A-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.1,0,w*0.2,h) img = img.crop(box) print('Intercepting the second part...') img.save('B'+i) print('B-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.2,0,w*0.3,h) img = img.crop(box) print('Intercepting the third part...') img.save('C'+i) print('C-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.3,0,w*0.4,h) img = img.crop(box) print('Intercepting part 4...') img.save('D'+i) print('D-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.4,0,w*0.5,h) img = img.crop(box) print('Intercepting part five...') img.save('E'+i) print('E-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.5,0,w*0.6,h) img = img.crop(box) print('Intercepting part VI...') img.save('F'+i) print('F-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.6,0,w*0.7,h) img = img.crop(box) print('Intercepting part VII...') img.save('G'+i) print('G-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.7,0,w*0.8,h) img = img.crop(box) print('Intercepting part 8...') img.save('H'+i) print('H-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.8,0,w*0.9,h) img = img.crop(box) print('Intercepting part 9...') img.save('I'+i) print('I-',i,'Saved successfully') for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) box = (w*0.9,0,w,h) img = img.crop(box) print('Intercepting part 10...') img.save('J'+i) print('J-',i,'Saved successfully') print("'{}'All pictures in the directory have been saved to the directory of this file.".format(path))
3. Traverse all the pictures in a folder and rotate them by width
Through the pilot library, we can also rotate the picture to the desired direction:
from PIL import Image import os path = 'X:\\file\\New folder' #File directory #The path directory needs to be changed manually after it is truncated path_list = os.listdir(path) print(path_list) for i in path_list: a = open(os.path.join(path,i),'rb') img = Image.open(a) w = img.width #Width of picture h = img.height #Height of picture print('Processing pictures',i,'wide',w,'long',h) if h > w: img.rotate(270, expand=True).save('0'+i) #Let's look at the rotate method in the pilot print('Rotation successful') print("'{}'All pictures in the directory have been saved to the directory of this file.".format(path))
reference resources
Common operations of pilot.image, such as image clipping, rotation, scaling, flipping, etc https://blog.csdn.net/weixin_42074867/article/details/90440294
Operate image 4-pilot-rotation, flip image, change single element https://blog.csdn.net/qq_36482772/article/details/53346511
Two ways of image clipping in Python -- pilot and opencv https://blog.csdn.net/hfutdog/article/details/82351549
python sequential reading method of pictures in folders https://blog.csdn.net/gbz3300255/article/details/108238083
Python gets the files and subfolders under the folder https://blog.csdn.net/JohinieLi/article/details/76660733