python 图片拼接

python 图片拼接

from typing import Any
from PIL import Image,ImageDraw
import numpy as np


# 把多张图片合并成一张6英寸的图片
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()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值