Python圖像處理之圖片拼接和堆疊案例教程
業(yè)務(wù)說明:
此示例腳本作用,包含方法和邏輯:圖像讀取,圖片尺寸讀取,重置圖片大小,圖片等比縮放,圖片拼接,圖片覆蓋與堆疊(子母圖)
圖片展示:
單張素材:
origin_image.jpg
result_image.jpg
face_image.jpg
拼接結(jié)果示例圖:
拼接和堆疊完成后示例:
拼接和堆疊完成后示例2:
拼接和堆疊完成后示例3:
代碼示例:
import os import time from os import listdir from PIL import Image from loguru import logger from PIL import Image def image_synthesis(mother_img, son_img, save_img, size_data, coefficient=2.5, coordinate=None): """ mother_img="C:/Users/Administrator/Desktop/QRCode/b.jpg", son_img="C:/Users/Administrator/Desktop/QRCode/y.png", save_img="C:/Users/Administrator/Desktop/QRCode/newimg.png", coordinate=None#如果為None表示直接將子圖在母圖中居中也可以直接賦值坐標(biāo) # coordinate=(50,50) :param mother_img: 母圖 :param son_img: 子圖 :param save_img: 保存圖片名 :param size_data: 母圖的高 :param coefficient: 子圖相對(duì)于母圖高度壓縮系數(shù) :param coordinate: 子圖在母圖的坐標(biāo) (50, 100)- (距離Y軸水平距離, 距離X軸垂直距離) :return: """ # 將圖片賦值,方便后面的代碼調(diào)用 M_Img = Image.open(mother_img) S_Img = Image.open(son_img) # 給圖片指定色彩顯示格式 M_Img = M_Img.convert("RGBA") # CMYK/RGBA 轉(zhuǎn)換顏色格式(CMYK用于打印機(jī)的色彩,RGBA用于顯示器的色彩) # 獲取圖片的尺寸 M_Img_w, M_Img_h = M_Img.size # 獲取被放圖片的大?。笀D) logger.info(f"母圖尺寸:{M_Img.size}") S_Img_w, S_Img_h = S_Img.size # 獲取小圖的大?。ㄗ訄D) logger.info(f"子圖尺寸:{S_Img.size}") son_resize_h = size_data / coefficient factor = son_resize_h / S_Img_h if son_resize_h > S_Img_h else S_Img_h / son_resize_h # 子圖縮小的倍數(shù)1代表不變,2就代表原來的一半 logger.info(f"子圖重置比例: {factor}") size_w = int(S_Img_w / factor) size_h = int(S_Img_h / factor) # 防止子圖尺寸大于母圖 if S_Img_w > size_w: logger.info(f"防止子圖尺寸大于母圖") S_Img_w = size_w if S_Img_h > size_h: logger.info(f"防止子圖尺寸大于母圖") S_Img_h = size_h # 重新設(shè)置子圖的尺寸 icon = S_Img.resize((S_Img_w, S_Img_h), Image.ANTIALIAS) logger.info(f"重置后子圖尺寸:{(S_Img_w, S_Img_h)}") try: if not coordinate or coordinate == "": w = int((M_Img_w - S_Img_w) / 2) h = int((M_Img_h - S_Img_h)) coordinate = (w, h) # 粘貼子圖到母圖的指定坐標(biāo)(當(dāng)前水平居中,垂直靠下) M_Img.paste(icon, coordinate, mask=None) else: logger.info("已經(jīng)指定坐標(biāo)") # 粘貼子圖到母圖的指定坐標(biāo)(指定坐標(biāo)) M_Img.paste(icon, coordinate, mask=None) except: logger.info("坐標(biāo)指定出錯(cuò) ") # 保存圖片 M_Img.save(save_img) return save_img def image_stitching(origin_img_path, result_img_path, output_img_path, size_data): # 獲取當(dāng)前文件夾中所有JPG圖像 # im_list = [Image.open(fn) for fn in listdir() if fn.endswith('.jpg')] origin_data = Image.open(origin_img_path) result_data = Image.open(result_img_path) M_Img_w, M_Img_h = origin_data.size # 獲取被放圖片的大小 logger.info(f"待拼接圖片的原尺寸: {(M_Img_w, M_Img_h)}") # 圖片轉(zhuǎn)化尺寸(注:此業(yè)務(wù)中,origin和result均為尺寸比例相同的圖片(寬高比相同的圖片)) factor = M_Img_h / size_data if size_data > M_Img_h else size_data / M_Img_h # 子圖縮小的倍數(shù)1代表不變,2就代表原來的一半 size_w = int(M_Img_w / factor) logger.info(f"待拼接圖片重置尺寸: {(size_w, size_data)}") origin_img = origin_data.resize((size_w, size_data), Image.BILINEAR) result_img = result_data.resize((size_w, size_data), Image.BILINEAR) image_list = [origin_img, result_img] # 單幅圖像尺寸 width, height = image_list[0].size logger.info(f"--- width = {width}, height = {height}") # 創(chuàng)建空白長圖 result = Image.new(image_list[0].mode, (width * len(image_list), height)) # # 拼接圖片 for i, im in enumerate(image_list): result.paste(im, box=(i * width, 0)) # 保存圖片 result.save(output_img_path) return stitching_img_path if __name__ == '__main__': """圖片拼接與堆疊合成腳本""" # root_path = './1000x966' root_path = './500x841' # root_path = './1000x667' size_data = 1280 # 原圖重制尺寸值 TODO 實(shí)現(xiàn)圖片重制大小的時(shí)候按比例進(jìn)行寬高的縮放 origin_img_path = os.path.join(root_path, 'origin_image.png') result_img_path = os.path.join(root_path, 'result_image.png') face_img_path = os.path.join(root_path, 'face_image.png') stitching_img_path = os.path.join(root_path, 'stitching_.png') # 兩圖左右拼接 last_img_path = image_stitching(origin_img_path, result_img_path, stitching_img_path, size_data) logger.info(f"左右拼接完成 ---") # 覆蓋小圖片到拼接圖居中靠下 synthesis_img_path = os.path.join(root_path, 'synthesis_.png') res = image_synthesis(last_img_path, face_img_path, synthesis_img_path, size_data, # coordinate=(100, 500) ) logger.info(f"--- end --- res = {res}")
到此這篇關(guān)于Python圖像處理之圖片拼接和堆疊案例教程的文章就介紹到這了,更多相關(guān)Python圖像處理之圖片拼接和堆疊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)暴力破解wifi密碼并打包成exe
python號(hào)稱是編程界的萬金油,那么是否可以做個(gè)讀取電腦網(wǎng)卡wifi并暴力破解的小腳本呢?在這個(gè)基礎(chǔ)上為了方便體驗(yàn)是不是可以將其打包成exe這樣方便執(zhí)行的小應(yīng)用呢?本文就來和大家一起聊聊2022-09-09科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)
這篇文章主要為大家介紹了科學(xué)Python開發(fā)環(huán)境Spyder必知必會(huì)點(diǎn)及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01使用python實(shí)現(xiàn)名片管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了使用python實(shí)現(xiàn)名片管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06使用python提取html文件中的特定數(shù)據(jù)的實(shí)現(xiàn)代碼
python提供了SGMLParser類用于html文件的解析。用戶只需從SGMLParser類繼承子類,并在子類中對(duì)html文件做具體處理2013-03-03Python 切片索引越界的問題(數(shù)組下標(biāo)越界)
Python語言處理字符串、數(shù)組類的問題時(shí)有一定概率需要使用切片方法,本文主要介紹了Python 切片索引越界的問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12python標(biāo)準(zhǔn)庫OS模塊函數(shù)列表與實(shí)例全解
這篇文章主要介紹了python標(biāo)準(zhǔn)庫OS模塊函數(shù)列表與實(shí)例全解,需要的朋友可以參考下2020-03-03python如何爬取動(dòng)態(tài)網(wǎng)站
在本篇內(nèi)容里小編給各位分享了關(guān)于python如何爬取動(dòng)態(tài)網(wǎng)站的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以參考下。2020-09-09Python常用的內(nèi)置序列結(jié)構(gòu)(列表、元組、字典)學(xué)習(xí)筆記
序列指元素排成一列的數(shù)據(jù)結(jié)構(gòu),Python中有l(wèi)ist列表而沒有默認(rèn)內(nèi)置array數(shù)組,以下我們來整理一下Python常用的內(nèi)置序列結(jié)構(gòu)(列表、元組、字典)學(xué)習(xí)筆記2016-07-07