python:圖片拼接


一:尺寸相同的圖片拼接

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:\Users\Administrator\Desktop\demopic"

# root文件夾的路徑  dirs 路徑下的文件夾列表  files路徑下的文件列表
for root, dirs, files in os.walk(dir_name):
    for file in files:
        if "jpg" in file:  # 子串在母串里面不
            all_path.append(os.path.join(root,file))

# all_path獲取每張圖片的絕對路徑

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


for i in range(row_max):
    for j in range(line_max):
        # 每次打開圖片絕對路路徑列表的第一張圖片
        pic_fole_head = Image.open(all_path[num])
        # 獲取圖片的尺寸
        wihth,height = pic_fole_head.size
        # 按照指定的尺寸,給圖片重新賦值,<PIL.Image.Image image mode=RGB size=200x200 at 0x127B7978>
        tmppic = pic_fole_head.resize((width_i, height_i))
        # 計算每個圖片的左上角的坐標點(0, 0),(0, 200),(0, 400),(200, 0),(200, 200)。。。。(400, 400)
        loc = (int(i % line_max * width_i), int(j % line_max * height_i))
        print("第{}張圖的存放位置".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')
 
        

 二:尺寸不相同的圖片進行拼接

例如:將這種圖,拼接成一幅完整的圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM