88 lines
3.3 KiB
Python
88 lines
3.3 KiB
Python
#!/bin/python
|
||
#coding: utf-8
|
||
# +-------------------------------------------------------------------
|
||
# | version:1.0
|
||
# +-------------------------------------------------------------------
|
||
# | django-vue-lyadmin 专业版
|
||
# +-------------------------------------------------------------------
|
||
# | Author: lybbn
|
||
# +-------------------------------------------------------------------
|
||
# | QQ: 1042594286
|
||
# +-------------------------------------------------------------------
|
||
|
||
# ------------------------------
|
||
# 条形码唛头生成--filters过滤
|
||
# ------------------------------
|
||
|
||
"""
|
||
@Remark: 生成条形码并转换为图片
|
||
|
||
ean13 只支持数字
|
||
code128 支持字符串
|
||
"""
|
||
|
||
import barcode
|
||
import base64
|
||
from barcode.writer import ImageWriter
|
||
import os
|
||
from django.conf import settings
|
||
import time
|
||
from PIL import Image, ImageDraw,ImageFont
|
||
from config import DOMAIN_HOST
|
||
from io import BytesIO
|
||
|
||
BARCODE_TYPE = 'code128'
|
||
|
||
CUSTOM_OPTIONS = {
|
||
"module_width": 0.2, # 单个条纹的最小宽度, mm
|
||
"module_height": 15.0, # 条纹带的高度, mm
|
||
"quiet_zone": 0, # 图片两边与首尾两条纹之间的距离, mm
|
||
"font_size": 7, # 条纹底部文本的大小,pt
|
||
"text_distance": 4.0, # 条纹底部与条纹之间的距离, mm
|
||
# "background": "rgba(0,0,0,0)", # 背景颜色,默认为白色white,设置为transparent需要ImageWriter的mode="RGBA"
|
||
}
|
||
|
||
def barCodeGenerate(content,imgpath=""):
|
||
"""
|
||
生成条形码唛头,返回base64内容
|
||
content:条形码内容
|
||
"""
|
||
|
||
if not imgpath:
|
||
imgpath = os.path.join(settings.MEDIA_ROOT, 'temp')
|
||
if not os.path.exists(imgpath):
|
||
os.makedirs(imgpath)
|
||
filename = str(int(time.time()*100000))
|
||
code = barcode.get_barcode_class(BARCODE_TYPE)
|
||
# code_img = code(content, writer=ImageWriter(mode="RGBA"))#透明背景
|
||
code_img = code(content, writer=ImageWriter()) # 透明背景
|
||
# 暂存条形码并记录完整路径
|
||
file_save_name = code_img.save(imgpath+os.path.sep+filename, options=CUSTOM_OPTIONS)
|
||
# 合成唛头
|
||
bg = Image.open(os.path.join(settings.MEDIA_ROOT, 'maitou.jpg'))
|
||
x, y = bg.size
|
||
bc = Image.open(file_save_name)
|
||
# x2, y2 = bc.size
|
||
# bc = bc.resize((int(x2*0.85), int(y2*0.85)))
|
||
x1, y1 = bc.size
|
||
bg.paste(bc, (int(x/2)-int(x1/2), int(y/2)-int(y1/2)+60))
|
||
draw = ImageDraw.Draw(bg)
|
||
thefontpath = os.path.join(settings.MEDIA_ROOT, 'SourceHanSansCN-Medium.otf')
|
||
fontstyle = ImageFont.truetype(thefontpath,50, encoding="utf-8")
|
||
draw.text((int(x/2)-int(x1/2)-42, int(y/2)-int(y1/2)-10), text=content,font=fontstyle, fill='black')
|
||
#返回http路径
|
||
# fullfilepath = imgpath + os.path.sep + filename + ".png"
|
||
# bg.save(fullfilepath)
|
||
# web_img_url = DOMAIN_HOST + settings.MEDIA_URL + "/temp/" + filename + ".png" # 绝对路径http://xxx.xxx.com/media/xxx/xxxx/xxx.png
|
||
# return web_img_url
|
||
#返回base64
|
||
output_buffer = BytesIO()
|
||
bg.save(output_buffer,quality=100, format="PNG")
|
||
byte_data = output_buffer.getvalue()
|
||
base64_str = 'data:image/png;base64,' + base64.b64encode(byte_data).decode("utf-8")
|
||
#移除暂存条形码文件
|
||
exist_file = os.path.exists(file_save_name)
|
||
if exist_file:
|
||
os.remove(file_save_name)
|
||
return base64_str
|