我在python中使用pyqrcode模块并使用它生成QR代码.如何将徽标放在QR码的中心.
代码看起来像这样
import pyqrcode
data = "Hello World!!"
number = pyqrcode.create(data)
number.png('xyz.png', scale=int(scale))
with open('xyz.png', "rb") as f:
return HttpResponse(f.read(), content_type="image/png")
Run Code Online (Sandbox Code Playgroud)
或者有没有其他方法来做这个而不是pyqrcode?
虽然这个问题已经存在一年多了,但我仍然发布我的解决方案,因为我希望它可以帮助其他人。
注意我生成了 png 格式的二维码图像。要使其正常工作,pypng必须安装模块。
import pyqrcode
from PIL import Image
# Generate the qr code and save as png
qrobj = pyqrcode.create('https://stackoverflow.com')
with open('test.png', 'wb') as f:
qrobj.png(f, scale=10)
# Now open that png image to put the logo
img = Image.open('test.png')
width, height = img.size
# How big the logo we want to put in the qr code png
logo_size = 50
# Open the logo image
logo = Image.open('stackoverflow-logo.jpg')
# Calculate xmin, ymin, xmax, ymax to put the logo
xmin = ymin = int((width / 2) - (logo_size / 2))
xmax = ymax = int((width / 2) + (logo_size / 2))
# resize the logo as calculated
logo = logo.resize((xmax - xmin, ymax - ymin))
# put the logo in the qr code
img.paste(logo, (xmin, ymin, xmax, ymax))
img.show()
Run Code Online (Sandbox Code Playgroud)
小智 6

import pyqrcode
from PIL import Image
url = pyqrcode.QRCode('http://www.eqxiu.com',error = 'H')
url.png('test.png',scale=10)
im = Image.open('test.png')
im = im.convert("RGBA")
logo = Image.open('logo.png')
box = (135,135,235,235)
im.crop(box)
region = logo
region = region.resize((box[2] - box[0], box[3] - box[1]))
im.paste(region,box)
im.show()
Run Code Online (Sandbox Code Playgroud)