python 图片拼接
from typing import Any
from PIL import Image,ImageDraw
import numpy as np
class SixInchImageMerge(object):
template = None
merge_style = None
width = 4000
height = 6000
remain = 100
imgs = []
percent = width / height
max_width = 0
max_heigth = 0
def __init__(self,imgs,merge_style,color="white") -> None:
self.template = Image.new('RGB', (self.width,self.height), color=color)
self.merge_style = merge_style
for img in imgs:
im = Image.open(img)
self.imgs.append(im)
"""
三张照片合并成一张6寸照片 前两张两寸 最后一张四寸
"""
def threeImageMerge(self):
self.max_width = int((self.width - (3 * self.remain)))
self.max_height = int((self.height - (3 * self.remain)))
if len(self.imgs) != 3:
raise("照片数量须是3个")
for idx,img in enumerate(self.imgs):
if idx in {0,1}:
width,height = self.inch2_resize(img=img)
im = self.imgs[idx].resize((width,height))
mwidth = width if width <= int(self.max_width / 2) else int(self.max_width / 2)
mgiht = height if height <= int(self.max_height / 3) else int(self.max_height / 3)
im = im.crop((0,0,mwidth,mgiht))
box = ((self.remain * idx + self.remain + (int(self.max_width / 2) * idx)),self.remain)
self.template.paste(im=im,box=box)
if idx == 2:
width,height = self.inch4_resize(img=img)
im = self.imgs[idx].resize((width,height))
mwidth = width if width <= int(self.max_width) else int(self.max_width)
mheight = height if height <= int(self.max_height / 3 * 2) else int(self.max_height / 3 * 2)
im = im.crop((0,0,mwidth,mheight))
box = (self.remain + int(self.remain / 2), int(self.max_height / 3) + (2 *self.remain))
self.template.paste(im=im,box=box)
self.template.show()
"""
九宫格图片拼接
"""
def nineImgaeMerge(self):
row,column = 3,3
self.max_width = int(self.width - 4 * self.remain)
self.max_height = int(self.height - 4 * self.remain)
self.reshape(row,column)
for idx,row in enumerate(self.imgs):
for jdx,col in enumerate(row):
width,height = self.nineInchResize(col)
im = col.resize((width,height))
mwidth = width if width <= int(self.max_width / 3) else int(self.max_width /3)
mheight = height if height <= int(self.max_height / 3 ) else int(self.max_height / 3)
im = im.crop((0,0,mwidth,mheight))
box = ((self.remain * jdx + self.remain + (int(self.max_width / 3) * jdx)),(self.remain * idx + self.remain + (int(self.max_height / 3) * idx)))
self.template.paste(im=im,box=box)
self.template.show()
def nineInchResize(self,img):
max_width = int(self.max_width / 3)
width,height = img.size
max_height = int((max_width / width) * height)
return max_width,max_height
def reshape(self,row,column):
tmp = []
for i in range(row):
tmp.append(self.imgs[i*column:(i+1) * column])
self.imgs = tmp
def inch2_resize(self,img):
max_width = int(self.max_width / 2)
width,height = img.size
max_height = int((max_width / width) * height)
return max_width,max_height
def inch4_resize(self,img):
max_width = int(self.max_width)
width,height = img.size
max_height = int((max_width / width) * height)
return max_width,max_height
@staticmethod
def test_threeImageMerge():
imgs = ["1.jpg","2.jpg","5.jpg"]
im = SixInchImageMerge(imgs=imgs,merge_style=None)
im.threeImageMerge()
@staticmethod
def test_nineImgaeMerge():
imgs = ["1.jpg","11.jpg","3.jpg","4.jpg","5.jpg","7.jpg","2.jpg","15.jpg","16.jpg"]
im = SixInchImageMerge(imgs=imgs,merge_style=None)
im.nineImgaeMerge()
if __name__ == "__main__":
SixInchImageMerge.test_threeImageMerge()
SixInchImageMerge.test_nineImgaeMerge()