用python編寫一個圖片拼接工具
更新時間:2022年01月24日 11:40:09 作者:Johnny An
大家好,本篇文章主要講的是用python編寫一個圖片拼接工具,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
前言

故事要從上面這張表情包開始講起,看到這張表情包之后,我突發(fā)奇想,覺得可以將室友上班摸魚的照片拼接起來,做成表情包叫他起床 激勵他學習?。?!于是我馬上行動起來,用 pillow庫隨便寫寫僅供娛樂!大佬勿噴!
為了保護室友隱私,將照片用小藍代替!

代碼展示
這里寫了兩種拼接方式,可以根據圖像比例自行調整。
又是不務正業(yè)的一天。。。
from PIL import Image
import matplotlib.pyplot as plt
def SpliceImage(img_1, img_2, save_img, mode=None):
'''
把兩張圖片左右拼接
img_1 : 輸入圖片1(左)的路徑
img_2 : 輸入圖片2(右)的路徑
save_img: 保存圖片的路徑
mode : 默認為 None ,寬度保持不變,高度取最大值
可設為'mean',寬度與高度均取兩張圖片的平均值
'''
# -----get width and height of the Images----- #
img1 = Image.open(img_1)
img2 = Image.open(img_2)
w1, h1 = img1.size
w2, h2 = img2.size
print("原始圖片1大?。簕} x {}" .format(w1,h1))
print("原始圖片2大?。簕} x {}" .format(w2,h2))
# -----resize image with high-quality----- #
if mode == 'mean':
width = int((w1 + w2) / 2)
height = int((h1 + h2) / 2)
w1 = int(width/2)
w2 = int(width/2)
else:
width = w1 + w2
height = max(h1,h2)
img1 = img1.resize((w1, height), Image.ANTIALIAS)
img2 = img2.resize((w2, height), Image.ANTIALIAS)
# -----create a new image-----#
img = Image.new("RGB", (width, height), (0,0,0))
img.paste(img1, (0,0))
img.paste(img2, (w1,0))
img.save(save_img)
print("輸出圖片大小:{} x {}" .format(width,height))
return img
if __name__ == '__main__':
img_1 = r'.\img\sleeper.PNG'
img_2 = r'.\img\dog.PNG'
save_img = r'.\img\getup.jpg'
try:
img = SpliceImage(img_1, img_2, save_img, mode='mean')
except:
print('Image file error!')
plt.imshow(img)
效果展示
針不戳!希望可以激勵室友努力學習,不再偷懶!hhhhhh

總結
到此這篇關于用python編寫一個圖片拼接工具的文章就介紹到這了,更多相關python圖片拼接內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python3中在Anaconda環(huán)境下安裝basemap包
今天小編就為大家分享一篇關于Python3中在Anaconda環(huán)境下安裝basemap包的文章,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Python面向對象程序設計OOP深入分析【構造函數,組合類,工具類等】
這篇文章主要介紹了Python面向對象程序設計OOP,較為詳細的深入分析了Python面向對象的構造函數,組合類,工具類等相關概念、使用方法及操作注意事項,需要的朋友可以參考下2019-01-01

