0
点赞
收藏
分享

微信扫一扫

python 节日祝福文字嵌套

东言肆语 2022-07-14 阅读 95


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

pip install pillow

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

from PIL import Image
from PIL import ImageDraw
from PIL import

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​​​文件夹下,字体通常有两种后缀名,​​ttc​​​和​​ttf​​。查看电脑内所有可用字体代码如下:

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()​​函数完成。


举报

相关推荐

0 条评论