Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How would I go about creating PNG templates that I could pass data or information to so that it would show in the Image? For clarification, I'm thinking of something similar to how GitHub README Stats works, but with PNGs instead of SVGs. Or how widgets work for Discord's Widget Images (e.g. 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).

question from:https://stackoverflow.com/questions/65945849/how-to-create-png-templates-with-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
158 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use 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...emplate.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).

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.

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.

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

Image references

template.png

template.png

images.jfif

images.jfif

out.png

out.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...