Python如何在二维码中间插入logo?
How to insert logo in the center of qrcode in Python?
我在 python 中使用 pyqrcode 模块并用它生成二维码。怎么把logo放在那个二维码的中间。
代码如下所示
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")
或者除了 pyqrcode 之外还有其他方法吗?
如果您使用高冗余算法(例如H),您可以将生成的QRCode 损坏到一定百分比。 H 表示你可以覆盖 30% 的数据,它仍然有效。
这意味着只需将您的图像放在代码上即可。格式由你决定。
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()
虽然这个问题已经存在 1 年多了,但我仍然在发布我的解决方案,希望它能对其他人有所帮助。
注意 我生成了png格式的二维码图片。要使其正常工作,必须安装 pypng
模块。
import pyqrcode
from PIL import Image
# Generate the qr code and save as png
qrobj = pyqrcode.create('https://whosebug.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('Whosebug-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()
这可以通过 css 轻松完成。将二维码容器 class 设置为 position: relative;
并在该容器中包含一个带有 position: absolute;
的 img / svg 并将其转换为中心。限制叠加层大小,防止二维码无法读取
https://jsfiddle.net/xf7d058c/
<!-- CSS -->
.container { width: 400px; height: 400px; border: 1px solid green; position: relative; }
.transform { position: absolute; left: 50%; top: 50%; transform:translate(-50%, -50%); }
<!-- HTML -->
<div class="container">
<svg height="20" width="20" class="transform"><circle cx="5" cy="5" r="5" fill="#802929" /></svg>
</div>
我在 python 中使用 pyqrcode 模块并用它生成二维码。怎么把logo放在那个二维码的中间。
代码如下所示
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")
或者除了 pyqrcode 之外还有其他方法吗?
如果您使用高冗余算法(例如H),您可以将生成的QRCode 损坏到一定百分比。 H 表示你可以覆盖 30% 的数据,它仍然有效。
这意味着只需将您的图像放在代码上即可。格式由你决定。
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()
虽然这个问题已经存在 1 年多了,但我仍然在发布我的解决方案,希望它能对其他人有所帮助。
注意 我生成了png格式的二维码图片。要使其正常工作,必须安装 pypng
模块。
import pyqrcode
from PIL import Image
# Generate the qr code and save as png
qrobj = pyqrcode.create('https://whosebug.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('Whosebug-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()
这可以通过 css 轻松完成。将二维码容器 class 设置为 position: relative;
并在该容器中包含一个带有 position: absolute;
的 img / svg 并将其转换为中心。限制叠加层大小,防止二维码无法读取
https://jsfiddle.net/xf7d058c/
<!-- CSS -->
.container { width: 400px; height: 400px; border: 1px solid green; position: relative; }
.transform { position: absolute; left: 50%; top: 50%; transform:translate(-50%, -50%); }
<!-- HTML -->
<div class="container">
<svg height="20" width="20" class="transform"><circle cx="5" cy="5" r="5" fill="#802929" /></svg>
</div>