python生成417条形码(PDF417)

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#-*- encoding: utf-8 -*-
from elaphe.pdf417 import Pdf417
import os
import pygame
import sys
from pygame.locals import *
from PIL import Image,ImageDraw,ImageFont

reload(sys)
sys.setdefaultencoding("utf-8")

#1 生成PDF147的条码图片
bc = Pdf417()
#print bc.render_ps_code('PDF417')
text = '068080835AD000D10701014111'.upper()
img = bc.render(text, options=dict(columns=1,rows=10,eclevel=1,rowmult=1), scale=3)
#img.save(text+'.png', 'png')

#2 生成描述性文字图片
#3 合成为一个新的图片,即最终包含说明的,符合菲亚特规范PDF147的条码图片

base = Image.open("pdf147-middle.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)
b_h = int((base_h - tmp_h) / 2) - 15
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)-b_w*2,95), text,(0,0,0),font=font)
content = "P07D111140 T07D1111407010 20150321113322 188007"
draw.text((base_h/2-len(content)-b_h*2,110),unicode(content,'utf-8'),(0,0,0),font=font)

base.save(text+".png")