问题描述
我目前正在尝试在标签(pylabels库)上绘制二维码.我可以使用qrcode库创建二维码图像.但是,我对如何在标签上实际绘制图像有些迷惑.
I am currently trying to draw qr codes on my labels (pylabels library). I am able to create the qr code images using the qrcode library. However, I am a bit lost with how I actually draw the images on the labels.
我使用了 https://github上的示例代码.com/bcbnz/pylabels/blob/1.2.1/demos/basic.py
我尝试使用shapes提供的所有方法.绘图但没有成功.
I tried to use all the methods offered by shapes.Drawing but haven't had any success.
我浏览了reportlab文档,但并不太了解如何使它工作.
I went through the reportlab documentation but doesn't really understand how I get it to work.
def draw_label(label, width, height, obj):
# Just convert the object to a string and print this at the bottom left of
# the label.
config = obj.get('config')
label.add(shapes.String(2, 2, f'{config.get("code")}', fontName="Helvetica", fontSize=10))
label.add(shapes.Drawing.drawOn(label, obj.get('image'), 100, 100))
def create_labels():
specs = labels.Specification(210, 297, 4, 5, 45, 45, corner_radius=1)
sheet = labels.Sheet(specs, draw_label, border=True)
for num in range(10000, 10020):
setup_dict = {
'setup': 'setup-config',
'code': num,
}
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=15,
border=5,
)
# Add JSON encoded data to qr code
qr.add_data(json.dumps(setup_dict))
qr.make(fit=True)
qr_code = {
'config': setup_dict,
'image': qr.make_image(fill_color="black", back_color="white"),
}
sheet.add_label(qr_code)
sheet.save('/qr_codes.pdf')
如果能给我一个例子,将不胜感激.
Would be very much appreciated, if anyone could give me an example.
推荐答案
我能够弄清楚.我很困惑,以至于看不到明显的东西.如果遇到相同的问题,下面是您所需要的.
I was able to figure it out. I was so confused, that I did not see the obvious. Below is what you need, if you run into the same issue.
from reportlab.graphics.shapes import Image
label.add(Image(25, 42, 80, 80, obj.get('image')))
这篇关于如何在标签上绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!