1
# Copyright (c) 2008 marscel.wordpress.com
3
# Copyright (c) 2010 SIA "KN dati". (http://kndati.lv) All Rights Reserved.
4
# General contacts <info@kndati.lv>
7
# Requires Python and Python Imaging Library (PIL),
8
# has been tested with Python v2.6 and PIL v1.1.6
11
# code39.py 100 2 "Hello World" barcode.png
13
# This creates a PNG image "barcode.png" containing a barcode of the height of 100px
14
# a min line width of 2px with "Hello World" encoded as "*HELLO WORLD*" in Code 39
16
import Image, ImageDraw, ImageFont, sys
17
from tools import config
24
'*':[0,3,0,1,2,1,2,1,0],
25
'-':[0,3,0,1,0,1,2,1,2],
26
'$':[0,3,0,3,0,3,0,1,0],
27
'%':[0,1,0,3,0,3,0,3,0],
28
' ':[0,3,2,1,0,1,2,1,0],
29
'.':[2,3,0,1,0,1,2,1,0],
30
'/':[0,3,0,3,0,1,0,3,0],
31
'+':[0,3,0,1,0,3,0,3,0],
32
'0':[0,1,0,3,2,1,2,1,0],
33
'1':[2,1,0,3,0,1,0,1,2],
34
'2':[0,1,2,3,0,1,0,1,2],
35
'3':[2,1,2,3,0,1,0,1,0],
36
'4':[0,1,0,3,2,1,0,1,2],
37
'5':[2,1,0,3,2,1,0,1,0],
38
'6':[0,1,2,3,2,1,0,1,0],
39
'7':[0,1,0,3,0,1,2,1,2],
40
'8':[2,1,0,3,0,1,2,1,0],
41
'9':[0,1,2,3,0,1,2,1,0],
42
'A':[2,1,0,1,0,3,0,1,2],
43
'B':[0,1,2,1,0,3,0,1,2],
44
'C':[2,1,2,1,0,3,0,1,0],
45
'D':[0,1,0,1,2,3,0,1,2],
46
'E':[2,1,0,1,2,3,0,1,0],
47
'F':[0,1,2,1,2,3,0,1,0],
48
'G':[0,1,0,1,0,3,2,1,2],
49
'H':[2,1,0,1,0,3,2,1,0],
50
'I':[0,1,2,1,0,3,2,1,0],
51
'J':[0,1,0,1,2,3,2,1,0],
52
'K':[2,1,0,1,0,1,0,3,2],
53
'L':[0,1,2,1,0,1,0,3,2],
54
'M':[2,1,2,1,0,1,0,3,0],
55
'N':[0,1,0,1,2,1,0,3,2],
56
'O':[2,1,0,1,2,1,0,3,0],
57
'P':[0,1,2,1,2,1,0,3,0],
58
'Q':[0,1,0,1,0,1,2,3,2],
59
'R':[2,1,0,1,0,1,2,3,0],
60
'S':[0,1,2,1,0,1,2,3,0],
61
'T':[0,1,0,1,2,1,2,3,0],
62
'U':[2,3,0,1,0,1,0,1,2],
63
'V':[0,3,2,1,0,1,0,1,2],
64
'W':[2,3,2,1,0,1,0,1,0],
65
'X':[0,3,0,1,2,1,0,1,2],
66
'Y':[2,3,0,1,2,1,0,1,0],
67
'Z':[0,3,2,1,2,1,0,1,0]
70
def create_c39(height, smallest, text):
74
machinetext = "*" + text + "*"
76
while i < len(machinetext):
77
char = machinetext[i].capitalize()
88
if seg == 0 or seg == 1:
89
pixel_length = pixel_length + smallest
91
elif seg == 2 or seg == 3:
92
pixel_length = pixel_length + smallest * 3
101
pixel_length = pixel_length + 2*marginx + len(newtext) * smallest
102
pixel_height = height + 2*marginy + fontsize
104
barcode_img = Image.new('RGB', [pixel_length, pixel_height], "white")
106
if len(seglist) == 0:
110
draw = ImageDraw.Draw(barcode_img)
113
while i < len(seglist):
115
color = (255, 255, 255)
118
if seg == 0 or seg == 2:
124
elif seg == 1 or seg == 3:
125
color = (255, 255, 255)
134
draw.line((current_x, marginy, current_x, marginy+height), fill=color)
135
current_x = current_x + 1
141
draw.line((current_x, marginy, current_x, marginy+height), fill=(255,255,255))
142
current_x = current_x + 1
146
font = ImageFont.truetype(config['addons_path']+"/report_aeroo/barcode/FreeMonoBold.ttf", fontsize)
148
draw.text((pixel_length/2 - len(newtext)*(fontsize/2)/2-len(newtext), height+fontsize), newtext, font=font, fill=0)