The following command line application will create a 128B barcode as an image file of your choice. Some of this code was found on the internet and the rest was created by the Webmaster. This application requires the Python Image Library. I haven't gotten around to generating an output image the hard way so PIL is still required. One trick to get around that is to use something like PY2EXE and create a stand alone executable. PIL will be wrapped up inside of the binary. Enjoy!
help_text = r"""
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
PY_BARCODE v1.0
Copyright 2010 - PSE Entertainment Corp
This small utility uses the Python Image Library (thanks Fredrick Lundh!)
to create a Code 128 Barcode using Character Set B. This set includes
most of the normal characters on your keyboard. The resulting barcode
image will be saved as a file of the following supported types:
jpg, jpeg, gif, png, tif, tiff, bmp
Usage:
py_barcode [string] [valid\path\to\output.png]
Examples:
py_barcode "Hello World" "c:\folder\barcode.png"
py_barcode "98503730-1" "outputbarcode.gif"
py_barcode "Punkin Ninni Rocks!" "c:\folder\subfolder\barcode.jpg"
NOTE: this help_text displays with ANY error. Check that the file path
really exists and that you're requesting one of the supported image types.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
"""
# courbB08.pil PIL Font file uuencoded
courB08_pil ="""eJztl91rFkcUxp+Zt7vGFYzVtiJKICgYlLRWkaBBVGgDraFGCH5gsQp+QMBqabAVRYJYAlakCkoh
CpYgxaLkIu1NvLBeSAStglpqL6xQAsVe2AuL5u2buH3mzGaYPf9AKWTl8d3nl7MzZ2bnazvea9+9
7+PurFWut5e0Zu+s7VybYfKavP7LK3X/5TlM4Q3/OWbyf1ARD/6mgb2SjwtPhbpnq0iKZ6ahrmCj
wqbxdgamRnHOA69jimN5zvIS8cDcUEeVdYzRAw1FHcJYXgPvG4s6Jlgj7xeEequS3wLeNvGvnrEO
tq+Jt82szT+b86+WHlgS2jHGuHF6YHnog1zaupxqCcy3t4X3rVG9iXhgjW+bsFQ80BaxRDywTrF1
VId6toPaqOI2UlsV20ptV2w7tUuxXVSXYl3UvoIZ9kFFPPBJ6D/HLD3QXbwjyDjI6YHPiz5FXiN7
SQ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tEG5LIShM4EHIGoLIRsHzcvEuGwMYA4DZPn7gP
MA1QIgltnt82cTu7j5n76mmz3TU5Bh3PFRTHku52aBgaTnJD7m1c0a3hNjbWWjBtMsP/OFac/LYA
NAAWepdYodB58NBFIuOjNSQ4cgXplqP2RyOe8fd999T8weqBRwLwNFdQobHgA1/YTV8PH+TwV59v
Bo7Y1J4rmHFv3T9e8rmmXdGSuPpSbBnhYJ7V8ICz6AfGcdTpRkpCUU8WcOT8wb+dSHIb6QZapx0M
Y2DO4i7jYV2AUNkkErpQFHVYmFRmYD7OJhDyQSiow4IkrS3TbpQqFA9slE4jnj6peXMTC+N8buJ2
0Uv5eOothuGIiluyCDtff3miBzJHjncOIC3bPT8FLabRPd0TCWy346Mmn9Rz23WyNMJcsnqhQani
3CMFOZuYU7c20zTNVqNbGPNxALWnybeLEcTvXWpc10leI5ae/CI9qBqI686cnO6P6F33e2vAp0nz
9+hnbNeueh/261UJK5aVeSf73ZSXA7dOBXvkXODEb9hVww4KtPNAbPvaZbi0q9kICCl+CiBJSzLv
a8TlntYlC4UHvCRTlaXOy13VAbN0eae2v3hNesWXLsWPkjfOPq7e6zd1fOfc1TckDaylrvleinnT
8Ui87ScLMVhhEx7SUJ8U2zKrRR2Z1dEqZlkr7kDTuhFjpkvse9ZXN0R9H+DlYA4TXVm6/kXDQMyT
eGnJFXlLlSgva5iLUEcbiyDzNqf4Wr9kKYVUIcY40DrnsW4E4zW9QxnHVYx+bo64mIskDWjZgCrq
eVQFrS7Sh/uFLftIidKWbgj6Oq652d4c3v88Dw2JDK7bSWX/ByuaLZI="""
def decodeFontFile(data, file):
""" Decode font file embedded in this script and create file """
from zlib import decompress
from base64 import decodestring
from os.path import exists
# If the font file is missing
if not exists(file):
# Write font file
open (file, "wb").write(decompress(decodestring(data)))
CharSetB = {
' ':0, '!':1, '"':2, '#':3, '$':4, '%':5, '&':6, "'":7,
'(':8, ')':9, '*':10, '+':11, ',':12, '-':13, '.':14, '/':15,
'0':16, '1':17, '2':18, '3':19, '4':20, '5':21, '6':22, '7':23,
'8':24, '9':25, ':':26, ';':27, '<':28, '=':29, '>':30, '?':31,
'@':32, 'A':33, 'B':34, 'C':35, 'D':36, 'E':37, 'F':38, 'G':39,
'H':40, 'I':41, 'J':42, 'K':43, 'L':44, 'M':45, 'N':46, 'O':47,
'P':48, 'Q':49, 'R':50, 'S':51, 'T':52, 'U':53, 'V':54, 'W':55,
'X':56, 'Y':57, 'Z':58, '[':59, '\\':60, ']':61, '^':62, '_':63,
'' :64, 'a':65, 'b':66, 'c':67, 'd':68, 'e':69, 'f':70, 'g':71,
'h':72, 'i':73, 'j':74, 'k':75, 'l':76, 'm':77, 'n':78, 'o':79,
'p':80, 'q':81, 'r':82, 's':83, 't':84, 'u':85, 'v':86, 'w':87,
'x':88, 'y':89, 'z':90, '{':91, '|':92, '}':93, '~':94, '\x7F':95,
'FNC3':96, 'FNC2':97, 'SHIFT':98, 'Code C':99, 'FNC4':100, 'Code A':101, 'FNC1':102, 'START A':103,
'START B':104, 'START C':105, 'STOP':106}
ValueEncodings ={
0:'11011001100', 1:'11001101100', 2:'11001100110',
3:'10010011000', 4:'10010001100', 5:'10001001100',
6:'10011001000', 7:'10011000100', 8:'10001100100',
9:'11001001000', 10:'11001000100', 11:'11000100100',
12:'10110011100', 13:'10011011100', 14:'10011001110',
15:'10111001100', 16:'10011101100', 17:'10011100110',
18:'11001110010', 19:'11001011100', 20:'11001001110',
21:'11011100100', 22:'11001110100', 23:'11101101110',
24:'11101001100', 25:'11100101100', 26:'11100100110',
27:'11101100100', 28:'11100110100', 29:'11100110010',
30:'11011011000', 31:'11011000110', 32:'11000110110',
33:'10100011000', 34:'10001011000', 35:'10001000110',
36:'10110001000', 37:'10001101000', 38:'10001100010',
39:'11010001000', 40:'11000101000', 41:'11000100010',
42:'10110111000', 43:'10110001110', 44:'10001101110',
45:'10111011000', 46:'10111000110', 47:'10001110110',
48:'11101110110', 49:'11010001110', 50:'11000101110',
51:'11011101000', 52:'11011100010', 53:'11011101110',
54:'11101011000', 55:'11101000110', 56:'11100010110',
57:'11101101000', 58:'11101100010', 59:'11100011010',
60:'11101111010', 61:'11001000010', 62:'11110001010',
63:'10100110000', 64:'10100001100', 65:'10010110000',
66:'10010000110', 67:'10000101100', 68:'10000100110',
69:'10110010000', 70:'10110000100', 71:'10011010000',
72:'10011000010', 73:'10000110100', 74:'10000110010',
75:'11000010010', 76:'11001010000', 77:'11110111010',
78:'11000010100', 79:'10001111010', 80:'10100111100',
81:'10010111100', 82:'10010011110', 83:'10111100100',
84:'10011110100', 85:'10011110010', 86:'11110100100',
87:'11110010100', 88:'11110010010', 89:'11011011110',
90:'11011110110', 91:'11110110110', 92:'10101111000',
93:'10100011110', 94:'10001011110', 95:'10111101000',
96:'10111100010', 97:'11110101000', 98:'11110100010',
99:'10111011110',100:'10111101110',101:'11101011110',
102:'11110101110',103:'11010000100',104:'11010010000',
105:'11010011100',106:'1100011101011'}
def makeBits(input_string):
#start code for charsetB
startbits = ValueEncodings[104]
#stop code for charsetB
stopbits = ValueEncodings[106]
bits = ''
checksum = 104
place = 1
for c in input_string:
#get character code
code = CharSetB[c]
char_bits = ValueEncodings[code]
bits = bits + char_bits
checksum = checksum + place*code
place = place + 1
#checksum calculation
checksum = checksum % 103
checksum_bits = ValueEncodings[checksum]
#put it all together
bits = startbits + bits + checksum_bits + stopbits
return bits
def makeBar(value, ext, savepath, height = 40):
import Image, ImageFont, ImageDraw
# Create a missing font file
decodeFontFile(courB08_pil ,"courB08.pil")
decodeFontFile(courB08_pbm ,"courB08.pbm")
# Get the bar code list
bits = makeBits(value)
# Create a new image
whitespace = 10
textpad = 10
im = Image.new("1",(len(bits)+whitespace+whitespace,(height)+textpad))
# Load font
font = ImageFont.load("courB08.pil")
# Create drawer
draw = ImageDraw.Draw(im)
# Erase image
draw.rectangle(((0,0),(im.size[0],im.size[1])),fill=256)
# Draw text
draw.text(((im.size[0]-font.getsize(value)[0])/2, height-2), value, font=font, fill=0)
# Draw the bar codes
for bit in range(len(bits)):
if bits[bit] == '1':
draw.rectangle(((bit+whitespace,0),(bit+whitespace,height-2)),fill=0)
# Save the result image
im.save(savepath)
if __name__=="__main__":
import sys, os
## sys.argv.append("crapola")
## sys.argv.append(r"crapola.png")
#print the help_text if there are any errors
if len(sys.argv) <> 3:
print help_text
sys.exit()
value = sys.argv[1]
outfile = sys.argv[2]
thepath = os.path.normpath(outfile)
ext = os.path.splitext(outfile)[1]
#set the extension appropriately per PIL
if ext.lower() == "jpg": ext = "jpeg"
if ext.lower() == "tif": ext = "tiff"
ext = ext.upper()
makeBar(value,ext,thepath)
[/code]