#-*- encoding: utf-8 -*-
from elaphe.datamatrix import DataMatrix
import os
import pygame
import sys
from pygame.locals import *
from PIL import Image,ImageDraw,ImageFont
reload(sys)
sys.setdefaultencoding("utf-8")
#1 生成DataMatrix的条码图片
bc = DataMatrix()
text = '068080835AD000D10701014111'.upper()
img = bc.render(text, options=dict(rows=32, columns=32))
#img.save(text+'.png', 'png')
#2 生成描述性文字图片
#3 合成为一个新的图片,即最终包含说明的,符合菲亚特规范PDF147的条码图片
base = Image.open("datamatrix_big_32_32.png")
base_w, base_h = base.size
tmp = img#Image.open(text+'_tmp.png')
tmp_w, tmp_h = tmp.size
tmp = tmp.resize((tmp_w, tmp_h), Image.ANTIALIAS)
b_w = int((base_w - tmp_w) / 2) - 90
b_h = int((base_h - tmp_h) / 2)
tmp = tmp.convert("RGBA")
base.paste(tmp, (b_w, b_h), tmp)
font = ImageFont.truetype('simsun.ttc',12)
draw = ImageDraw.Draw(base)
draw.text((base_w/2-len(text) - 25,4), '0-68080835AD',(0,0,0),font=font)
draw.text((base_w/2-len(text) - 25,22), '000-D1',(0,0,0),font=font)
draw.text((base_w/2-len(text) - 25,40), '07010',(0,0,0),font=font)
draw.text((base_w/2-len(text) - 25,58), '1-4111',(0,0,0),font=font)
content = "P68080835AD T07D1111407010 20150321113322 188007"
draw.text((base_w/2-len(text) + 55,4), 'P68080835AD',(0,0,0),font=font)
draw.text((base_w/2-len(text) + 55,22), 'T07D1111407010',(0,0,0),font=font)
draw.text((base_w/2-len(text) + 55,40), '21/03/2015 16:42',(0,0,0),font=font)
draw.text((base_w/2-len(text) + 55,58), '188007',(0,0,0),font=font)
base.save(text+".png")