72 lines
1.8 KiB
Python
72 lines
1.8 KiB
Python
from PIL import Image, ImageDraw,ImageFont
|
|
from barcode import Code128
|
|
from barcode.writer import ImageWriter
|
|
import qrcode
|
|
|
|
# 定义图像大小和条形码大小
|
|
image_width = 2479
|
|
image_height = 3508
|
|
qrcode_size = 250
|
|
# 创建一个空白图像
|
|
image = Image.new("RGB", (image_width, image_height), (255, 255, 255))
|
|
draw = ImageDraw.Draw(image)
|
|
|
|
# 条形码配置
|
|
barcode_options = {
|
|
"writer": ImageWriter(),
|
|
"output": 'temp', # 输出格式,可以是文件名或字节流
|
|
}
|
|
|
|
# 在图像上生成多个条形码并放置它们
|
|
x_position = 50
|
|
y_position = 50
|
|
|
|
|
|
# 生成条形码
|
|
barcode = Code128("test", writer=barcode_options["writer"])
|
|
barcode_file = barcode.save(barcode_options["output"])
|
|
|
|
# 打开生成的条形码图像
|
|
barcode_image = Image.open(barcode_file)
|
|
|
|
# 将条形码添加到图像上
|
|
image.paste(barcode_image, (x_position, y_position))
|
|
|
|
# 更新下一个条形码的位置
|
|
y_position += 120
|
|
|
|
# 在条形码下方添加自定义文本
|
|
custom_text = "Custom Text 123"
|
|
text_color = (0, 0, 0) # 文本颜色(黑色)
|
|
# 设置字体大小
|
|
font_size = 100
|
|
font = ImageFont.truetype("fonts/arial", font_size) # 替换为你自己的字体文件路径
|
|
|
|
# 创建一个Draw对象并将文本添加到图像上
|
|
text_draw = ImageDraw.Draw(image)
|
|
text_draw.text((x_position, y_position + 120), custom_text, fill=text_color, font=font)
|
|
|
|
# 更新下一个条形码的位置
|
|
y_position += 220
|
|
|
|
|
|
# 生成二维码
|
|
qr = qrcode.QRCode(
|
|
version=1,
|
|
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
|
box_size=10,
|
|
border=4,
|
|
)
|
|
qr.add_data("QR Code Data456")
|
|
qr.make(fit=True)
|
|
|
|
qr_image = qr.make_image(fill_color="black", back_color="white").resize((qrcode_size, qrcode_size))
|
|
|
|
# 将二维码添加到图像上
|
|
image.paste(qr_image, (x_position, y_position))
|
|
# 保存生成的图像
|
|
#image.save("barcodes.png")
|
|
|
|
# 显示图像(可选)
|
|
image.show()
|