python: Image stitching

A: The same size image stitching

Import os
 from PIL Import Image 

width_i = 200 
height_i = 200 

row_max = 3 
LINE_MAX = 3 

all_path = List () 
NUM = 0 
pic_max = LINE_MAX * row_max 

dir_name = r " C: \ the Users \ Administrator \ Desktop \ demopic " 

# root folder files in the folder path dirs path to the list of files in the list of paths 
for root, dirs, files in os.walk (dir_name):
     for file in files:
         IF  " JPG"  In File:   # substring is not in string inside the mother 
            all_path.append (os.path.join (root, File)) 

# all_path get the absolute path of each picture 

toImage = Image.new ( ' RGBA ' , (width_i * LINE_MAX , height_i * row_max)) 


for i in the Range (row_max):
     for J in the Range (LINE_MAX):
         # every time you open a list of images path absolute path of the first picture 
        pic_fole_head = Image.open (all_path [NUM])
         # get picture the size 
        wihth, height = pic_fole_head.size
         #According to the dimensions specified, re-assignment to the picture, <PIL.Image.Image the RGB Image size = 200x200 MODE = AT 0x127B7978> 
        tmppic = pic_fole_head.resize ((width_i, height_i))
         # is calculated for each picture in the upper left corner coordinate point ( 0, 0), (0, 200), (0, 400), (200, 0), (200, 200). . . . (400, 400) 
        LOC = (int (* width_i I LINE_MAX%), int (* J LINE_MAX% height_i))
         Print ( " first storage position Photo {} of " .format (NUM), LOC) 
        toImage.paste ( tmppic, LOC) 
        NUM = NUM +. 1 IF NUM> = len (all_path):
             Print ( " breadk " )
             BREAK IF NUM> =

        
     pic_max:
        break

print(toImage.size)
toImage.save('merged.png')
 

 II: size is not the same picture mosaic

For example: FIG such, assembled into a complete FIG.

 

Guess you like

Origin www.cnblogs.com/meloncodezhang/p/11604764.html