如何在实际条形码Python`code128`模块中包含条形码值 [英] How to include barcode value with actual barcode Python `code128` module
问题描述
我刚刚构建了一个快速的Python Azure函数,该函数可以生成条形码.响应仅是.png格式的渲染条形码.我还需要在其下方显示条形码VALUE.
示例:
导入日志记录将azure.functions导入为func导入代码128导入io从PIL导入图像条码参数='1234'条码字节数= io.BytesIO()logging.info('#####正在生成条形码... #####')条码= code128.image(条码参数,高度= 100).保存(条码字节,"PNG")条码_字节数.搜索(0)logging.info('#####条码成功生成#####')返回func.HttpResponse(Barcode_bytes.getvalue(),status_code = 200,mimetype ='image/png')Barcode_bytes.close()
生成:
需要:海鹰之战
如何使用 code128
库将条形码值添加到条形码中?
文档中未显示任何选项
要生成的代码:
导入代码128导入io从PIL导入Image,ImageDraw,ImageFont#获取条形码值Barcode_param ='SUFFERINSUCCOTASH'#创建条形码图像条码图像= code128.image(条码参数,高度= 100)#为条形码和文本创建空图像top_bott_margin = 70l_r_margin = 10new_height =条码图像高度+(2 * top_bott_margin)new_width =条码图像宽度+(2 * l_r_margin)new_image = Image.new('RGB',(new_width,new_height),(255,255,255))#将条形码放在新图像上条码_y = 100new_image.paste(条形码图像,(0,条形码_y))#绘制文字的对象画= ImageDraw.Draw(new_image)#定义自定义文本的大小和字体h1_size = 28h2_size = 28h3_size = 16footer_size = 21h1_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)#定义自定义文本company_name ='是的!公司"id1 = '11 -22-33-44'license_num ='为什么要同步!'product_type ='GRADE A GREATNESS'center_product_type =(条形码图像宽度/2)-len(product_type)* 5center_barcode_value =(barcode_image.width/2)-len(barcode_param)* 8#在图片上画文字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)),条码参数,fill =(0,0,0),font = h2_font)#保存在文件中new_image.save('barcode_image.png','PNG')#在默认查看器中显示导入网络浏览器webbrowser.open('barcode_image.png')
感谢队友!
您将代码获取为枕头图像,因此可以使用枕头为文本添加边距并绘制文本.
您可以获取原始尺寸
w,h =条码图像大小
计算新尺寸
new_w = w#相同保证金= 20new_h = h +(2 *保证金)
创建带有白色背景的空图像
new_image = Image.new('RGB',(new_w,new_h),(255,255,255))
将原始条形码放在高度的中间
new_image.paste(条形码图像,(0,边距))
接下来,您可以使用 ImageDraw
创建可以绘制对象或在图像上放置文本的对象
draw = ImageDraw.Draw(new_image)
,然后您可以使用 text()
放置一些文本.您可能需要使用 ImageFont
来加载字体和设置大小.我使用默认字体和大小.
#fnt = ImageFont.truetype("arial.ttf",40)draw.text((10,new_h-10),条码文本,fill =(0,0,0))#,font = fnt)
,您在 new_image
中有带有文字的图片.您可以将其保存在文件中,然后直接在网络浏览器中进行检查,也可以转换为字节并发送给客户端.
在示例中,我仅使用标准模块 webbrowser
来检查图像.
编辑
正如@RufusVS在评论中指出的,我可以使用 image_new.show()
代替 webbrowser
导入代码128导入io从PIL导入Image,ImageDraw,ImageFont条码参数='1234'条形码_文本='theseahawksarewinning'#原始图片条码图像= code128.image(条码参数,高度= 100)#代码和文字为空的图片-文字需要空白w,h =条码图像大小保证金= 20new_h = h +(2 *保证金)new_image = Image.new('RGB',(w,new_h),(255,255,255))#将条形码放在新图像上new_image.paste(条形码图像,(0,边距))#绘制文字的对象画= ImageDraw.Draw(new_image)#绘制文字#fnt = ImageFont.truetype("arial.ttf",40)draw.text((10,new_h-10),条码文本,fill =(0,0,0))#,font = fnt)##保存在文件中new_image.save('barcode_image.png','PNG')#在默认查看器中显示导入网络浏览器webbrowser.open('barcode_image.png')#---稍后发送-条码字节数= io.BytesIO()new_image.save(条形码字节,"PNG")条码_字节数.搜索(0)数据=条码字节数.getvalue()
文档:图像 Image.new(), ImageDraw , ImageDraw.text() ImageFont
I just built a quick Python Azure Function that generates a barcode. The response is ONLY the rendered barcode in .png format. I also need the barcode VALUE to be displayed below it.
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?
There are no options shown in the docs.
EDIT 1: After @Furas great example, I now have:
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.
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
draw = ImageDraw.Draw(new_image)
and you can put some text using text()
. You may need to use ImageFont
to load font and set size. 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
. And you can save it in file and check directly in web browser or you can convert to bytes and send to client.
In example I use standard module webbrowser
only to check image.
EDIT
As @RufusVS noted in comment I could use image_new.show()
instead of 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
这篇关于如何在实际条形码Python`code128`模块中包含条形码值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!