Recently I was writing a paper on Convolutional Neural Networks. There are many experimental results that need to be sorted out. I originally used Meitu Xiuxiu for image stitching, but found that there were too many repeated operations and the stitching effect was not good. I thought of writing a script in python. To achieve, see a simple example:
Horizontal stitching
First, I need to splice the deformation of the same picture into one line, the code is as follows:
import os
from PIL import Image
UNIT_SIZE =229 #The size of a single image is 229*229
TARGET_WIDTH =6* UNIT_SIZE #The horizontal length after splicing is 6*229
path ="C:/Users/zm/Desktop/FinalResult/Other-Reconstruction/2"
images =[] #Store the names of all images first
for root, dirs, files in os.walk(path):for f in files :
images.append(f)for i inrange(len(images)/6): #6 images as a group
imagefile =[]
j =0for j inrange(6):
imagefile.append(Image.open(path+'/'+images[i*6+j]))
target = Image.new('RGB',(TARGET_WIDTH, UNIT_SIZE))
left =0
right = UNIT_SIZE
for image in imagefile:
target.paste(image,(left,0, right, UNIT_SIZE))#Copy image to the specified location of target
left += UNIT_SIZE #left is the abscissa of the upper left corner, increasing sequentially
right += UNIT_SIZE #right is the abscissa of the lower right, increasing sequentially
quality_value =100 #quality to specify the quality of the generated image, the range is 0-100
target.save(path+'/result/'+os.path.splitext(images[i*6+j])[0]+'.jpg', quality = quality_value)
imagefile =[]
Vertical stitching
After stitching each type of pictures into a row of six pictures, stitch these pictures together vertically. There are 3 kinds of images in total, so there are 3 rows
import os
from PIL import Image
UNIT_SIZE =229 #Image height
TARGET_WIDTH =6* UNIT_SIZE #There are 6 images in a row, so 6*229 so wide
path ="C:/Users/zm/Desktop/FinalResult/Other-Reconstruction/2/result"
imagefile =[]for root, dirs, files in os.walk(path):for f in files :
imagefile.append(Image.open(path+'/'+f))
target = Image.new('RGB',(TARGET_WIDTH, UNIT_SIZE*3)) #The size of the final stitched image is(229*3)*(229*6)
left =0
right = UNIT_SIZE
for image in imagefile:
target.paste(image,(0, left, TARGET_WIDTH, right))
left += UNIT_SIZE #Stitching from top to bottom, the ordinate of the upper left corner increases
right += UNIT_SIZE #The ordinate in the lower left corner also increases
quality_value =100
target.save(path+'/result.jpg', quality = quality_value)
Simultaneous horizontal and vertical stitching
The pictures that need to be processed today are as follows: on the left is the folder where the pictures are located, and the pictures in each folder are shown on the right as →_→, which need to be spliced into 2*5 pictures.
The script written is as follows:
import os
from PIL import Image
UNIT_SIZE =229 # the size of image
def pinjie(images,num):
target = Image.new('RGB',(UNIT_SIZE*5, UNIT_SIZE*2)) # result is 2*5
leftone =0
lefttwo =0
rightone = UNIT_SIZE
righttwo = UNIT_SIZE
for i inrange(len(images)):if(i%2==0):
target.paste(images[i],(leftone,0, rightone, UNIT_SIZE))
leftone += UNIT_SIZE #Move the upper left corner of the first line to the right
rightone += UNIT_SIZE #Move right bottom corner
else:
target.paste(images[i],(lefttwo, UNIT_SIZE, righttwo, UNIT_SIZE*2))
lefttwo += UNIT_SIZE #Move the upper left corner of the second line to the right
righttwo += UNIT_SIZE #Move right bottom corner
quality_value =100
target.save(path+dirlist[num]+'.jpg', quality = quality_value)
path ="C:/Users/laojbdao/Desktop/FinalResult/result4/different_distribution/"
dirlist =[] # all dir name
for root, dirs, files in os.walk(path):for dir in dirs :
dirlist.append(dir)
num =0for dir in dirlist:
images =[] # images in each folder
for root, dirs, files in os.walk(path+dir): # traverse each folder
print path+dir+''for file in files:
images.append(Image.open(path+dir+'/'+file))pinjie(images,num)
num +=1
images =[]
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