【python】图片背景透明化、在背景图上添加图片、在图片上添加文字

目录

一、需求

二、思路

三、程序


一、需求

        目前有这样一个需求,就是希望在一个背景图上添加另一张图片,并且添加一些文字信息,因此我写了如下一个类。

二、思路

        由于在背景图上添加的图片不是透明背景的图片,因此需要先转成透明背景的图片,这里写了一个transparent_back()方法。然后就是在背景图上添加图片,这里写了addImg()方法。最后添加文字的需求就写了addText()方法。

三、程序

import cv2
from PIL import ImageFont, ImageDraw, Image
import numpy as np

class Trans:
    def __init__(self):
        # 图片大小
        self.QRCode_width = 220
        self.QRCode_height = 220
        # 图片在背景图上的左上角位置
        self.QRCode_position_x = 260
        self.QRCode_position_y = -5

        self.fontpath_Bold = "font/simhei.ttf"  # seguibl.ttf seguibli.ttf seguiemj.ttf seguihis.ttf seguili.ttf seguisb.ttf seguisbi.ttf seguisli.ttf seguisym.ttf
        self.fontpath_Normal = "font/simhei.ttf"
        self.font_Bold_size = 36
        self.font_Normal_size = 18

        self.backgrondImg = "resource/images/background.png"  # 背景图

    def transparent_back(self, img):  # 图片背景透明化
        img = img.convert('RGBA')
        L, H = img.size
        color_0 = img.getpixel((0, 0))
        for h in range(H):
            for l in range(L):
                dot = (l, h)
                color_1 = img.getpixel(dot)
                if color_1 == color_0:
                    color_1 = color_1[:-1] + (0,)
                    img.putpixel(dot, color_1)
        return img

    def addImg(self, QRCodeImg):  # 在背景图上添加一张图
        markImg = Image.open(QRCodeImg)
        transparent_img = self.transparent_back(markImg)  # 二维码背景透明化
        transparent_img = transparent_img.resize((self.QRCode_width, self.QRCode_height))  # 调整二维码尺寸
        r, g, b, a = transparent_img.split()  # 获取alpha通道
        img = Image.open(self.backgrondImg)
        img.paste(transparent_img, (self.QRCode_position_x, self.QRCode_position_y), mask=a)  # 粘贴二维码
        return img

    def addText(self, img, bianHao, peiDuiMa):  # 在图上添加文字
        fontpath_Bold = self.fontpath_Bold
        fontpath_Normal = self.fontpath_Normal
        font_Bold = ImageFont.truetype(fontpath_Bold, size=self.font_Bold_size, encoding="utf-8")
        font_Normal = ImageFont.truetype(fontpath_Normal, size=self.font_Normal_size, encoding="utf-8")
        cvImg = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)  # PIL转cv
        img_pil = Image.fromarray(cvImg)
        draw = ImageDraw.Draw(img_pil)
        # 绘制文字信息
        draw.text((10, 7), "编 号:"+bianHao, font=font_Bold, fill=(255, 255, 255))
        draw.text((10, 48), "配对码:"+peiDuiMa, font=font_Bold, fill=(255, 255, 255))
        draw.text((10, 90), "产品型号:xxx", font=font_Normal, fill=(255, 255, 255))
        draw.text((10, 117), "输  入:DC5V/2A", font=font_Normal, fill=(255, 255, 255))
        draw.text((10, 144), "电池容量:xxxx", font=font_Normal, fill=(255, 255, 255))
        draw.text((10, 171), "xxx公司", font=font_Normal, fill=(255, 255, 255))
        bk_img = np.array(img_pil)
        return bk_img


if __name__ == '__main__':
    TransObject = Trans()
    img = TransObject.addImg("BW050901_048088.png")  # 背景图和二维码
    bk_img = TransObject.addText(img, "BW050301", "047952")
    cv2.imshow("add_text", bk_img)
    cv2.waitKey()

猜你喜欢

转载自blog.csdn.net/ChaoChao66666/article/details/131132242