python實(shí)現(xiàn)兩張圖片左右(橫向)和縱向(上下)拼接組合-
- 主要用于對(duì)兩幅圖像進(jìn)行左右組合或者上下組合,詳細(xì)代碼如下:
from PIL import Image
def comb(png1, png2, style='horizontal'):
img1, img2 = Image.open(png1), Image.open(png2)
# 統(tǒng)一圖片尺寸,可以自定義設(shè)置(寬,高)
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')
- 左右組合
- 上下組合
文章來源地址http://www.zghlxwxcb.cn/news/detail-557240.html
文章來源:http://www.zghlxwxcb.cn/news/detail-557240.html
到了這里,關(guān)于python實(shí)現(xiàn)兩張圖片左右(橫向)和縱向(上下)拼接組合的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!