如何将条形码值包含在实际条形码Python`code128`模块中

如何将条形码值包含在实际条形码Python`code128`模块中,python,barcode,code128,Python,Barcode,Code128,我刚刚构建了一个快速生成条形码的Python Azure函数。响应仅为.png格式的呈现条形码。我还需要条形码值显示在它下面 示例: import logging import azure.functions as func import code128 import io from PIL import Image barcode_param = '1234' barcode_bytes = io.BytesIO() logging.info('##### Generating barc

我刚刚构建了一个快速生成条形码的Python Azure函数。响应仅为.png格式的呈现条形码。我还需要条形码值显示在它下面

示例:

import logging
import azure.functions as func
import code128
import io
from PIL import Image


barcode_param = '1234'
barcode_bytes = io.BytesIO()

logging.info('##### Generating barcode... #####')
barcode = code128.image(barcode_param, height=100).save(barcode_bytes, "PNG")
barcode_bytes.seek(0)
logging.info('##### Barcode successfully generated #####')
return func.HttpResponse(
    barcode_bytes.getvalue(),
    status_code=200,
    mimetype='image/png'
    )
    barcode_bytes.close()
import code128
import io
from PIL import Image, ImageDraw, ImageFont

# Get barcode value
barcode_param = 'SUFFERINSUCCOTASH'

# Create barcode image
barcode_image = code128.image(barcode_param, height=100)

# Create empty image for barcode + text
top_bott_margin = 70
l_r_margin = 10
new_height = barcode_image.height + (2 * top_bott_margin)
new_width = barcode_image.width + (2 * l_r_margin)
new_image = Image.new( 'RGB', (new_width, new_height), (255, 255, 255))

# put barcode on new image
barcode_y = 100
new_image.paste(barcode_image, (0, barcode_y))

# object to draw text
draw = ImageDraw.Draw(new_image)

# Define custom text size and font
h1_size = 28
h2_size = 28
h3_size = 16
footer_size = 21

h1_font = ImageFont.truetype("DejaVuSans-Bold.ttf", h1_size)
h2_font = ImageFont.truetype("Ubuntu-Th.ttf", h2_size)
h3_font = ImageFont.truetype("Ubuntu-Th.ttf", h3_size)
footer_font = ImageFont.truetype("UbuntuMono-R.ttf", footer_size)

# Define custom text
company_name = 'YAY! CORP.'
id1 = '11-22-33-44'
license_num = 'WHY SOYNTENLY!'
product_type = 'GRADE A GREATNESS'
center_product_type = (barcode_image.width / 2) - len(product_type) * 5
center_barcode_value = (barcode_image.width / 2) - len(barcode_param) * 8

# Draw text on picture
draw.text( (l_r_margin, 0), company_name, fill=(0, 0, 0), font=h1_font)
draw.text( (l_r_margin, h1_size), id1, fill=(0, 0, 0), font=h2_font)
draw.text( (l_r_margin + 2, (h1_size + h2_size + 5)), license_num, fill=(0, 0, 0), font=h3_font)
draw.text( (center_product_type, (h1_size + h2_size + h3_size)), product_type, fill=(0, 0, 0), font=footer_font)
draw.text( (center_barcode_value, (new_height - footer_size - 15)), barcode_param, fill=(0, 0, 0), font=h2_font)

# save in file 
new_image.save('barcode_image.png', 'PNG')

# show in default viewer
import webbrowser
webbrowser.open('barcode_image.png')
生成:

需要: 这些作业正在进行中

如何使用
code128
库将条形码值添加到条形码中

没有显示任何选项

编辑1:After@Furas伟大的例子,我现在有:

import logging
import azure.functions as func
import code128
import io
from PIL import Image


barcode_param = '1234'
barcode_bytes = io.BytesIO()

logging.info('##### Generating barcode... #####')
barcode = code128.image(barcode_param, height=100).save(barcode_bytes, "PNG")
barcode_bytes.seek(0)
logging.info('##### Barcode successfully generated #####')
return func.HttpResponse(
    barcode_bytes.getvalue(),
    status_code=200,
    mimetype='image/png'
    )
    barcode_bytes.close()
import code128
import io
from PIL import Image, ImageDraw, ImageFont

# Get barcode value
barcode_param = 'SUFFERINSUCCOTASH'

# Create barcode image
barcode_image = code128.image(barcode_param, height=100)

# Create empty image for barcode + text
top_bott_margin = 70
l_r_margin = 10
new_height = barcode_image.height + (2 * top_bott_margin)
new_width = barcode_image.width + (2 * l_r_margin)
new_image = Image.new( 'RGB', (new_width, new_height), (255, 255, 255))

# put barcode on new image
barcode_y = 100
new_image.paste(barcode_image, (0, barcode_y))

# object to draw text
draw = ImageDraw.Draw(new_image)

# Define custom text size and font
h1_size = 28
h2_size = 28
h3_size = 16
footer_size = 21

h1_font = ImageFont.truetype("DejaVuSans-Bold.ttf", h1_size)
h2_font = ImageFont.truetype("Ubuntu-Th.ttf", h2_size)
h3_font = ImageFont.truetype("Ubuntu-Th.ttf", h3_size)
footer_font = ImageFont.truetype("UbuntuMono-R.ttf", footer_size)

# Define custom text
company_name = 'YAY! CORP.'
id1 = '11-22-33-44'
license_num = 'WHY SOYNTENLY!'
product_type = 'GRADE A GREATNESS'
center_product_type = (barcode_image.width / 2) - len(product_type) * 5
center_barcode_value = (barcode_image.width / 2) - len(barcode_param) * 8

# Draw text on picture
draw.text( (l_r_margin, 0), company_name, fill=(0, 0, 0), font=h1_font)
draw.text( (l_r_margin, h1_size), id1, fill=(0, 0, 0), font=h2_font)
draw.text( (l_r_margin + 2, (h1_size + h2_size + 5)), license_num, fill=(0, 0, 0), font=h3_font)
draw.text( (center_product_type, (h1_size + h2_size + h3_size)), product_type, fill=(0, 0, 0), font=footer_font)
draw.text( (center_barcode_value, (new_height - footer_size - 15)), barcode_param, fill=(0, 0, 0), font=h2_font)

# save in file 
new_image.save('barcode_image.png', 'PNG')

# show in default viewer
import webbrowser
webbrowser.open('barcode_image.png')

要生成的代码:

import logging
import azure.functions as func
import code128
import io
from PIL import Image


barcode_param = '1234'
barcode_bytes = io.BytesIO()

logging.info('##### Generating barcode... #####')
barcode = code128.image(barcode_param, height=100).save(barcode_bytes, "PNG")
barcode_bytes.seek(0)
logging.info('##### Barcode successfully generated #####')
return func.HttpResponse(
    barcode_bytes.getvalue(),
    status_code=200,
    mimetype='image/png'
    )
    barcode_bytes.close()
import code128
import io
from PIL import Image, ImageDraw, ImageFont

# Get barcode value
barcode_param = 'SUFFERINSUCCOTASH'

# Create barcode image
barcode_image = code128.image(barcode_param, height=100)

# Create empty image for barcode + text
top_bott_margin = 70
l_r_margin = 10
new_height = barcode_image.height + (2 * top_bott_margin)
new_width = barcode_image.width + (2 * l_r_margin)
new_image = Image.new( 'RGB', (new_width, new_height), (255, 255, 255))

# put barcode on new image
barcode_y = 100
new_image.paste(barcode_image, (0, barcode_y))

# object to draw text
draw = ImageDraw.Draw(new_image)

# Define custom text size and font
h1_size = 28
h2_size = 28
h3_size = 16
footer_size = 21

h1_font = ImageFont.truetype("DejaVuSans-Bold.ttf", h1_size)
h2_font = ImageFont.truetype("Ubuntu-Th.ttf", h2_size)
h3_font = ImageFont.truetype("Ubuntu-Th.ttf", h3_size)
footer_font = ImageFont.truetype("UbuntuMono-R.ttf", footer_size)

# Define custom text
company_name = 'YAY! CORP.'
id1 = '11-22-33-44'
license_num = 'WHY SOYNTENLY!'
product_type = 'GRADE A GREATNESS'
center_product_type = (barcode_image.width / 2) - len(product_type) * 5
center_barcode_value = (barcode_image.width / 2) - len(barcode_param) * 8

# Draw text on picture
draw.text( (l_r_margin, 0), company_name, fill=(0, 0, 0), font=h1_font)
draw.text( (l_r_margin, h1_size), id1, fill=(0, 0, 0), font=h2_font)
draw.text( (l_r_margin + 2, (h1_size + h2_size + 5)), license_num, fill=(0, 0, 0), font=h3_font)
draw.text( (center_product_type, (h1_size + h2_size + h3_size)), product_type, fill=(0, 0, 0), font=footer_font)
draw.text( (center_barcode_value, (new_height - footer_size - 15)), barcode_param, fill=(0, 0, 0), font=h2_font)

# save in file 
new_image.save('barcode_image.png', 'PNG')

# show in default viewer
import webbrowser
webbrowser.open('barcode_image.png')

谢谢你,伙计

您可以将代码作为枕头图像,这样您就可以使用枕头为文本添加边距并绘制此文本

你可以得到原版的尺寸

w, h = barcode_image.size
计算新尺寸

new_w = w  # the same 

margin = 20
new_h = h + (2*margin) 
创建带有白色背景的空图像

new_image = Image.new('RGB', (new_w, new_h), (255, 255, 255))

将原始条形码放在高度

new_image.paste(barcode_image, (0, margin))
接下来,您可以使用
ImageDraw
创建可以绘制对象或在图像上放置文本的对象

draw = ImageDraw.Draw(new_image)
您可以使用
text()
放置一些文本。您可能需要使用
ImageFont
加载字体并设置大小。我使用默认字体和大小

#fnt = ImageFont.truetype("arial.ttf", 40)
draw.text( (10, new_h - 10), barcode_text, fill=(0, 0, 0))#, font=fnt) 
new\u image
中有带文本的图像。您可以将其保存在文件中并直接在web浏览器中进行检查,也可以将其转换为字节并发送到客户端

在示例中,我仅使用标准模块
webbrowser
检查图像


编辑

正如@RufusVS在评论中指出的,我可以使用
image\u new.show()
而不是
webbrowser



文件: , ,

可能您找不到它的选项,因为它不存在。您可以独立打印文本吗?您必须生成条形码数据的图像,并将其附加到条形码图形图像的下方。它可能不是code128库中的函数。需要更多的研究。您也可以使用,即
new_image.show()
来显示图形(在开发过程中)@RufusVS good point-我将此添加到了答案中。