简体   繁体   English

如何使用 Python 创建 PNG 模板

[英]How to create PNG templates with Python

How would I go about creating PNG templates that I could pass data or information to so that it would show in the Image?我 go 如何创建我可以将数据或信息传递到的 PNG 模板,以便它显示在图像中? For clarification, I'm thinking of something similar to how GitHub README Stats works, but with PNGs instead of SVGs.为了澄清,我正在考虑类似于GitHub README Stats的工作方式,但使用 PNG 而不是 SVG。 Or how widgets work for Discord's Widget Images (eg https://discordapp.com/api/guilds/guildID/widget.png?style=banner1 ).或者小部件如何用于 Discord 的小部件图像(例如https://discordapp.com/api/guilds/guildID/widget.png?style=banner1 )。

If there isn't a library for this kind of thing what would it take to make one?如果没有这种东西的图书馆,需要什么来制作一个? (I need a time sink, so I'm pretty keen on making something, even if it only fits my needs). (我需要一个时间沉没,所以我非常热衷于制作一些东西,即使它只适合我的需要)。

You can use PIL你可以使用PIL

from PIL import Image, ImageDraw, ImageFont #Import PIL functions
class myTemplate(): #Your template
    def __init__(self, name, description, image):
        self.name=name #Saves Name input as a self object
        self.description=description #Saves Description input as a self object
        self.image=image #Saves Image input as a self object
    def draw(self):
        """
        Draw Function
        ------------------ 
        Draws the template
        """
        img = Image.open(r'C:\foo\...\template.png', 'r').convert('RGB') #Opens Template Image
        if self.image != '':
            pasted = Image.open(self.image).convert("RGBA") #Opens Selected Image
            pasted=pasted.resize((278, int(pasted.size[1]*(278/pasted.size[0])))) #Resize image to width fit black area's width
            pasted=pasted.crop((0, 0, 278, 322)) #Crop height
            img.paste(pasted, (31, 141)) #Pastes image into template
            imgdraw=ImageDraw.Draw(img) #Create a canvas
        font=ImageFont.truetype("C:/Windows/Fonts/Calibril.ttf", 48) #Loads font
        imgdraw.text((515,152), self.name, (0,0,0), font=font) #Draws name
        imgdraw.text((654,231), self.description, (0,0,0), font=font) #Draws description

        img.save(r'C:\foo\...\out.png') #Saves output

amaztemp=myTemplate('Hello, world!', 'Hi there', r'C:\foo\...\images.jfif')
amaztemp.draw()

Explanation解释

PIL is an Image Manipulation library, it can edit images like GIMP with Python (but its more limited). PIL 是一个图像处理库,它可以像 GIMP 一样使用 Python 编辑图像(但更受限制)。

In this code we declare a class called myTemplate , that will be our template, inside this class we have two functions, one will initialize the class, and request name , description and image , and the other will draw.在这段代码中,我们声明了一个名为 myTemplate 的myTemplate ,这将是我们的模板,在这个 class 中,我们有两个函数,一个将初始化 class,并请求namedescriptionimage ,另一个将绘制。

Well, from line 13 to 15 , the program imports and verifies if there is a selected image, if yes it crops and resizes the selected image ( 16 and 17 ), then pastes the selected image in the template.那么,从第13行到第15行,程序导入并验证是否有选定的图像,如果有,它会裁剪并调整选定图像的大小(第1617行),然后将选定图像粘贴到模板中。

After this, name and description are drawn, then the program saves the file.在此之后,绘制名称和描述,然后程序保存文件。

You can customize teh class and the line 26 for your needs您可以根据需要自定义 teh class 和第26

Image references图片参考

template.png模板.png

模板.png

images.jfif图片.jfif

图片.jfif

out.png输出.png

输出.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM