add datamatrix generator
This commit is contained in:
parent
ca2de265b0
commit
61d9714ca3
|
@ -0,0 +1,116 @@
|
|||
from pylibdmtx import pylibdmtx as dmtx
|
||||
from PIL import Image, ImageDraw, ImageOps
|
||||
|
||||
tgt = 'cmdsite/polls/static/polls/barcode.png'
|
||||
# barcode min value = 0
|
||||
# barcode max value = 999999 (999,999) = 1 million values
|
||||
val = '564321'
|
||||
#barcode = dmtx.encode(val)
|
||||
#barcodeimg = Image.frombytes('RGB', (barcode.width, barcode.height), barcode.pixels)
|
||||
# img.convert('1') # convert rgb to monochrome
|
||||
# img.save('barcode.png')
|
||||
|
||||
white = (255, 255, 255)
|
||||
black = (0, 0, 0)
|
||||
|
||||
nullimg = Image.new('RGB', (9, 15), white)
|
||||
|
||||
# Draw zero
|
||||
zero = nullimg.copy()
|
||||
draw = ImageDraw.Draw(zero)
|
||||
draw.rectangle([(0, 0), (8, 14)], fill=white, outline=black, width=3)
|
||||
|
||||
# Draw one
|
||||
one = nullimg.copy()
|
||||
draw = ImageDraw.Draw(one)
|
||||
draw.rectangle([(3, 0), (5, 14)], fill=black, outline=black, width=3)
|
||||
|
||||
# Draw eight
|
||||
eight = zero.copy()
|
||||
draw = ImageDraw.Draw(eight)
|
||||
draw.rectangle([(3, 6), (5, 8)], fill=black, outline=black, width=3)
|
||||
|
||||
# Draw two and three
|
||||
two = eight.copy()
|
||||
draw = ImageDraw.Draw(two)
|
||||
draw.rectangle([(0, 3), (2, 5)], fill=white, outline=white, width=3)
|
||||
three = two.copy() # copy to three
|
||||
draw.rectangle([(6, 9), (8, 11)], fill=white, outline=white, width=3)
|
||||
# Finish three
|
||||
draw = ImageDraw.Draw(three)
|
||||
draw.rectangle([(0, 9), (2, 11)], fill=white, outline=white, width=3)
|
||||
|
||||
# Draw nine
|
||||
nine = eight.copy()
|
||||
draw = ImageDraw.Draw(nine)
|
||||
draw.rectangle([(0, 9), (5, 14)], fill=white, outline=white, width=3)
|
||||
|
||||
# draw four
|
||||
four = nine.copy()
|
||||
draw = ImageDraw.Draw(four)
|
||||
draw.rectangle([(3, 0), (5, 2)], fill=white, outline=white, width=3)
|
||||
|
||||
# draw five
|
||||
five = eight.copy()
|
||||
draw = ImageDraw.Draw(five)
|
||||
draw.rectangle([(6, 3), (8, 5)], fill=white, outline=white, width=3)
|
||||
draw.rectangle([(0, 9), (2, 11)], fill=white, outline=white, width=3)
|
||||
|
||||
# draw six
|
||||
six = eight.copy()
|
||||
draw = ImageDraw.Draw(six)
|
||||
draw.rectangle([(3, 0), (8, 5)], fill=white, outline=white, width=3)
|
||||
|
||||
# draw seven
|
||||
seven = eight.copy()
|
||||
draw = ImageDraw.Draw(seven)
|
||||
draw.rectangle([(0, 3), (5, 14)], fill=white, outline=white, width=3)
|
||||
|
||||
tgt = 'cmdsite/polls/static/polls/'
|
||||
|
||||
# create 'font'
|
||||
chars = [zero, one, two, three, four, five, six, seven, eight, nine]
|
||||
|
||||
def dmxcreate(value):
|
||||
val = str(value)
|
||||
# create value image
|
||||
valimg = Image.new('RGB',
|
||||
(((9 * len(val)) + (3 * (len(val) - 1))), 15),
|
||||
white)
|
||||
for i, x in enumerate(val):
|
||||
valimg.paste(chars[int(x)], ((9 * i) + (3 * i), 0))
|
||||
|
||||
# create datamatrix
|
||||
datamatrix = dmtx.encode(val.encode('utf-8'))
|
||||
dmximg = Image.frombytes('RGB',
|
||||
(datamatrix.width, datamatrix.height),
|
||||
datamatrix.pixels)
|
||||
|
||||
# combine datamatrix with text value
|
||||
img = Image.new('RGB',
|
||||
(max([dmximg.width, valimg.width]),
|
||||
dmximg.height + valimg.height),
|
||||
white)
|
||||
img.paste(dmximg, (round(abs((dmximg.width - img.width) / 2)), 0))
|
||||
# img.paste(dmximg)
|
||||
img.paste(valimg, (round(abs((valimg.width - img.width) / 2)),
|
||||
dmximg.height))
|
||||
# img.paste(valimg, (0, dmximg.height))
|
||||
return img
|
||||
|
||||
# combine images
|
||||
#finalimg = Image.new('RGB', (barcodeimg.width, barcodeimg.height + 15), white)
|
||||
#finalimg.paste(barcodeimg)
|
||||
|
||||
# add transparency
|
||||
# finalimage = barcodeandnumbers.copy()
|
||||
# finalimage.putalpha(ImageOps.invert(barcodeandnumbers).convert('L'))
|
||||
# finalimage.save(tgt + 'imagename.png')
|
||||
|
||||
# digit size = 9px X 15px
|
||||
# numbervalue width = 9 * len(numbervalue) + ((len(numbervalue) - 1) * 3)
|
||||
# barcode size = 80px x 80px
|
||||
# total height of finalimage = 98px
|
||||
# total width = (80 | 84 | 96 | 108)px
|
||||
# total width digits = (1-6 | 7 | 8 | 9)
|
||||
|
Loading…
Reference in New Issue