简体   繁体   English

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

[英]How to include barcode value with actual barcode Python `code128` module

I just built a quick Python Azure Function that generates a barcode.我刚刚构建了一个快速的 Python Azure Function 生成条形码。 The response is ONLY the rendered barcode in.png format.响应仅为 .png 格式的呈现条码。 I also need the barcode VALUE to be displayed below it.我还需要在其下方显示条形码 VALUE。

Example:例子:

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()

Generates:生成: 在此处输入图像描述

Need:需要: 在此处输入图像描述 theseahawksarewinning这些鹰派正在获胜

How can I add the barcode value to the barcode with the code128 library?如何使用code128库将条形码添加到条形码?

There are no options shown in the docs .文档中没有显示选项。

EDIT 1: After @Furas great example, I now have:编辑 1:在@Furas 很好的例子之后,我现在有:

在此处输入图像描述

Code to produce:生成代码:

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')

Thanks mate!谢了哥们!

You get code as Pillow image so you can use Pillow to add margin for text and to draw this text.您将代码作为 Pillow 图像,因此您可以使用 Pillow 为文本添加边距并绘制此文本。

You can get original size您可以获得原始尺寸

w, h = barcode_image.size

calculate new size计算新尺寸

new_w = w  # the same 

margin = 20
new_h = h + (2*margin) 

create empty image with white background创建具有白色背景的空图像

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

put original barcode in the middle of height将原始条形码放在高度的中间

new_image.paste(barcode_image, (0, margin))

Next you can use ImageDraw to create object which can draw objects or put text on image接下来,您可以使用ImageDraw创建 object 可以绘制对象或在图像上放置文本

draw = ImageDraw.Draw(new_image)

and you can put some text using text() .您可以使用text()放置一些文本。 You may need to use ImageFont to load font and set size.您可能需要使用ImageFont来加载字体和设置大小。 I use default font and size.我使用默认字体和大小。

#fnt = ImageFont.truetype("arial.ttf", 40)
draw.text( (10, new_h - 10), barcode_text, fill=(0, 0, 0))#, font=fnt) 

and you have image with text in new_image .并且您在new_image中有带有文本的图像。 And you can save it in file and check directly in web browser or you can convert to bytes and send to client.您可以将其保存在文件中并直接在 web 浏览器中查看,也可以转换为字节发送给客户端。

In example I use standard module webbrowser only to check image.在示例中,我仅使用标准模块webbrowser来检查图像。

在此处输入图像描述


EDIT编辑

As @RufusVS noted in comment I could use image_new.show() instead of webbrowser正如@RufusVS 在评论中指出的那样,我可以使用image_new.show()而不是webbrowser


import code128
import io
from PIL import Image, ImageDraw, ImageFont

barcode_param = '1234'
barcode_text = 'theseahawksarewinning'

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

# empty image for code and text - it needs margins for text
w, h = barcode_image.size
margin = 20
new_h = h +(2*margin) 

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

# put barcode on new image
new_image.paste(barcode_image, (0, margin))

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

# draw text
#fnt = ImageFont.truetype("arial.ttf", 40)
draw.text( (10, new_h - 10), barcode_text, fill=(0, 0, 0))#, font=fnt)  # 

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

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

# --- later send it---

barcode_bytes = io.BytesIO()
new_image.save(barcode_bytes, "PNG")
barcode_bytes.seek(0)
data = barcode_bytes.getvalue()

Doc: Image Image.new() , ImageDraw , ImageDraw.text() ImageFont文档: Image Image.new() , ImageDraw , ImageDraw.text() ImageFont

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM