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='vertical')
- Combination of left and right
- Combination up and down