如何在标签上绘制图像
How to draw images on labels
我目前正在尝试在我的标签(pylabels 库)上绘制二维码。我能够使用 qrcode 库创建二维码图像。然而,我对如何在标签上实际绘制图像有点迷茫。
我使用了 https://github.com/bcbnz/pylabels/blob/1.2.1/demos/basic.py
中的示例代码
我尝试使用了shapes.Drawing提供的所有方法,但没有成功。
我浏览了 reportlab 文档,但并不真正理解我是如何让它工作的。
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')
如果有人能给我一个例子,我将不胜感激。
我想通了。我很困惑,以至于我没有看到明显的东西。如果您 运行 遇到同样的问题,以下是您需要的。
from reportlab.graphics.shapes import Image
label.add(Image(25, 42, 80, 80, obj.get('image')))
我目前正在尝试在我的标签(pylabels 库)上绘制二维码。我能够使用 qrcode 库创建二维码图像。然而,我对如何在标签上实际绘制图像有点迷茫。
我使用了 https://github.com/bcbnz/pylabels/blob/1.2.1/demos/basic.py
中的示例代码我尝试使用了shapes.Drawing提供的所有方法,但没有成功。
我浏览了 reportlab 文档,但并不真正理解我是如何让它工作的。
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')
如果有人能给我一个例子,我将不胜感激。
我想通了。我很困惑,以至于我没有看到明显的东西。如果您 运行 遇到同样的问题,以下是您需要的。
from reportlab.graphics.shapes import Image
label.add(Image(25, 42, 80, 80, obj.get('image')))