Python realizes stitching pictures

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

Python realizes stitching pictures
Python implements horizontal stitching of pictures
Python realizes spaceship war
Python realizes horizontal and vertical splicing of pictures
Python generates connotative pictures
Python implements image stitching
python+OpenCV realizes image stitching
Python realizes online translation
Python realizes tank battle
Python3 realizes airplane war game
Python realizes online translation function
Python realizes apple eating game
Python realizes the guessing game
Python realizes 3D map visualization
Python realizes face sign-in system
Python implements panoramic image stitching
python opencv for image stitching
Python realizes batch naming photos
Python implements image stitching function
Python realizes business card management system
Python3 realizes business card management system
Python realizes express price query system
Python realizes online microblog data visualization
Python realizes image recognition car function
Python realizes udp transmission picture function
Python realizes console output color font
Python dry goods | remote sensing image stitching
Python dry goods | remote sensing image stitching
Python realizes batch modification of file names
python3 realizes the function of mask drawing
Python realizes the greedy snake double battle
Python- crawl all pictures of a station