公众号 自动生成海报 python_Python|自制二维码生成器

from tkinter import *

from tkinter.filedialog import *

from PIL import Image,ImageTk

import qrcode

def openfile():

global filename,image_name

filename = askopenfilename()

image_name = Image.open(filename)

image_name = image_name.resize((200, 200), Image.ANTIALIAS)#缩放图片

im_root = ImageTk.PhotoImage(image_name)  # 预设打开的图片

canvas1.create_image(100,100,image=im_root)  # 嵌入预设的图片

canvas1.place(x = 50,y = 100,width = 200,height = 200)

root.mainloop()

def creat():

global img

qr = qrcode.QRCode(

version=2,

error_correction=qrcode.constants.ERROR_CORRECT_Q,

box_size=10,

border=1)

url = entry1.get()

qr.add_data(url)

qr.make(fit=True)

img = qr.make_image()

img = img.convert("RGBA")

icon = image_name

icon = icon.convert("RGBA")

imgWight, imgHeight = img.size

iconWight = int(imgWight / 3)

iconHeight = int(imgHeight / 3)

icon = icon.resize((iconWight, iconHeight), Image.ANTIALIAS)

posW = int((imgWight - iconWight) / 2)

posH = int((imgHeight - iconHeight) / 2)

img.paste(icon, (posW, posH), icon)

img1 = img.resize((200, 200), Image.ANTIALIAS)

im_root = ImageTk.PhotoImage(img1)  # 预设打开的图片

canvas2.create_image(100,100,image=im_root)  # 嵌入预设的图片

canvas2.place(x = 360,y = 100,width = 200,height = 200)

root.mainloop()

def savefile():

pathname = asksaveasfilename(defaultextension = '.png',initialfile = '新的二维码.png')

img.save(pathname)

root = Tk()

root.title("二维码生成器")

root.geometry('600x400+400+100')

button1 = Button(root,text = '选择图标',font = ('宋体',20),fg = 'green',bg = 'white',command = openfile)#设置按钮

button2 = Button(root,text = '保存二维码',font = ('宋体',20),fg = 'green',bg = 'white',command = savefile)#设置按钮

button1.place(x = 90,y = 330,width = 120,height = 50)#显示按钮

button2.place(x = 385,y = 330,width = 150,height = 50)#显示按钮

label1 = Label(root,text = '输入链接',font = ('宋体',20),fg = 'black',bg = 'white')#设置组件

label1.place(x = 235,y = 5,width = 130,height = 50)

entry1 = Entry(root,font = ('宋体',20))#设置输入框

entry1.place(x = 50,y = 60,width = 510,height = 30)#显示组件

canvas1 = Canvas(root,width = 300,height = 300,bg = "white")#创建画布

canvas2 = Canvas(root,width = 300,height = 300,bg = "white")#创建画布

canvas1.place(x = 50,y = 100,width = 200,height = 200)

canvas2.place(x = 360,y = 100,width = 200,height = 200)

button = Button(root,text = '生成',font = ('宋体',15),fg = 'black',bg = 'pink',command = creat)#设置按钮

button.place(x = 280,y = 200,width = 50,height = 40)#显示按钮

root.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值