Python realizes the left and right (horizontal) and vertical (up and down) stitching and combination of two pictures

Python realizes the left and right (horizontal) and vertical (up and down) stitching and combination of two pictures-

  • It is mainly used to combine two images left and right or up and down. The detailed code is as follows:
from PIL import Image

def comb(png1, png2, style='horizontal'):

    img1, img2 = Image.open(png1), Image.open(png2)
    # 统一图片尺寸,可以自定义设置(宽,高)
    img1 = img1.resize((1500, 1000), Image.ANTIALIAS)
    img2 = img2.resize((1500, 1000), Image.ANTIALIAS)
    size1, size2 = img1.size, img2.size
    if style == 'horizontal':
        joint = Image.new('RGB', (size1[0] + size2[0], size1[1]))
        loc1, loc2 = (0, 0), (size1[0], 0)
        joint.paste(img1, loc1)
        joint.paste(img2, loc2)
        joint.save('horizontal.jpg')
    elif style == 'vertical':
        joint = Image.new('RGB', (size1[0], size1[1] + size2[1]))
        loc1, loc2 = (0, 0), (0, size1[1])
        joint.paste(img1, loc1)
        joint.paste(img2, loc2)
        joint.save('vertical.jpg')

if __name__ == '__main__':
    # 两张图片地址:
    png1 = r"./3.jpg"
    png2 = r"./4.jpg"
    # 左右拼接
    # comb(png1, png2, style='horizontal')

    # 上下拼接
    comb(png1, png2, style='vertical')
    
  • Combination of left and right
    insert image description here
  • Combination up and down
    insert image description here

Guess you like

Origin blog.csdn.net/crist_meng/article/details/130293510
Recommended