如何使用python-PIL在png图像上编写文本

2024-05-15 23:05:31 发布

您现在位置:Python中文网/ 问答频道 /正文

首先用sqli数据库制作一个数据库注册和数据库代码桂。这个用于从数据库获取数据并将数据文本写入png的表单图像。那个png图片写入数据后就是身份证。在

**所以这就是问题所在..代码运行时没有错误。但是图像未写入数据先试试这段代码,我没有使用数据库数据。我用的是字符串变量**

import sqlite3
from tkinter import Tk, Button, Canvas
from PIL import Image, ImageFont, ImageDraw

connection = sqlite3.connect("school.db")

tao = Tk()
tao.title("Mayurapada Central Collage")
tao.configure(bg = '#6699ff')
canvas = Canvas(tao,width = 600,height = 400,bg = '#6699ff')

def imgs():
  img = Image.open("C:\\Users\\TAO\\Desktop\\New\\02.png")
  #img.show()
  str01 = "Hello World"
  font = ImageFont.truetype("arial.ttf",200)
  w,h = font.getsize(str01)
  print(str01)

draw = ImageDraw.Draw(img)
draw.text(((900-w)/2,(900-h)/2),str01,font = font,fill = "black")
img.show()


button01 = Button(tao,text = "Preview",bd = 7,padx = 5,pady = 5,command = 
imgs).place(x = 50,y = 300)

canvas.pack()
tao.mainloop()

Tags: 数据代码from图像import数据库imgpng