python 节日祝福文字嵌套

所需模块:pillow
安装方法:Win + R 打开运行界面,输入cmd点击确定进入终端界面,使用如下命令安装pillow模块。

pip install pillow

NB(注意): 这里pillow模块安装时要使用pillow,导入是则使用PIL。
模块导入方法如下:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

Image:提供了加载图像和创建新图像的功能
ImageDraw: 提供了创建新图像、对现有图像进行注释或润色的功能
ImageFont: 提供了字体选择功能

使用设计者模式书写代码,代码如下:

class newYear:
    def __init__(self):
        self.word_size = 25
        self.word1 = "新"
        self.word2 = "年"
        self.word3 = "快"
        self.word4 = "乐"
        self.image1 = None
        self.image2 = None
        self.image3 = None
        self.image4 = None
        self.component1 = None
        self.component2 = None
    
    def imageDraw(self, word):
        image1 = Image.new("RGB", (self.word_size, self.word_size), "white").convert("L")

        work_bench = ImageDraw.Draw(im=image1)

        work_bench.text(xy=(0, 0), text=u'{}'.format(word), fill="#000000", font=ImageFont.truetype('C:/Windows/Fonts/simsun.ttc', self.word_size))

        # image1.save("C:/Users/15025/Desktop/{}.jpg".format(word), "JPEG")
        return image1                         
    
    def createComponent(self, image1, image2):
        bench_size = (pow(self.word_size, 2), pow(self.word_size, 2))
        new_bench = Image.new('L', bench_size, "white")
        for w in range(image1.width):
            for h in range(image1.height):
                if image1.getpixel((w, h)) < 127:
                    new_bench.paste(image2, (w * image2.width, h * image2.height))
        
        return new_bench
        
    def mergeComponent(self):
        bench_size = (1300, 800)

        new_bench = Image.new('L', bench_size, "white")
        
        new_bench.paste(self.component1, (0, 0))
        new_bench.paste(self.component2, (700, 0))
        
        new_bench.save("C:/Users/15025/Desktop/blessing.jpg")
        
    def createBlessing(self):
        self.image1 = self.imageDraw(self.word1)
        self.image2 = self.imageDraw(self.word2)
        self.image3 = self.imageDraw(self.word3)
        self.image4 = self.imageDraw(self.word4)
        
        self.component1 = self.createComponent(self.image1, self.image3)
        self.component2 = self.createComponent(self.image2, self.image4)
        
        self.mergeComponent()
        


class wordBlessing:
    unit_dict = {
        "newyear": newYear
    }

    @classmethod
    def create_unit(cls, unit_type):
        return cls.unit_dict.get(unit_type)()


class App:
    blessing = wordBlessing

    def blessingMake(self, blessing_type):
        unit = self.blessing.create_unit(blessing_type)
        unit.createBlessing()


if __name__ == "__main__":
    app = App()
    app.blessingMake("newyear")

首先因为需要中文输出,字体选择上不能够随意选择,只能够选择适合中文输出的字体,如这里使用的是simsun.ttc字体格式,通常字体库位于我们电脑的C:/Windows/Fonts/中的Fonts文件夹下,字体通常有两种后缀名,ttcttf。查看电脑内所有可用字体代码如下:

import matplotlib.font_manager
print(matplotlib.font_manager.findSystemFonts(fontpaths=None))

可以自行尝试其他格式的字体,这里不做过多尝试。
该程序总体思路是创建所需要的汉字的单个图片,尺寸均为25 * 25,将字存放在变量self.word1中,将字存放在变量self.word2中,将字存放在变量self.word3中,将字存放在变量self.word4中,由imageDraw()函数完成。之后因为要进行文字嵌套,我们知道字应该嵌套字,字应该嵌套字,所以我们需要将字组合起来并存储到变量self.component1中,同理将字组合存储到self.component2,这一部分由函数createComponent()完成。最终我们需要将两个component组合起来放置在一个画布图像的固定位置,形成最后的图像,由mergeComponent()函数完成。

猜你喜欢

转载自blog.csdn.net/u011699626/article/details/108281934