cleanup datamatrix generator
This commit is contained in:
parent
61d9714ca3
commit
f79ed45dd3
81
barcode.py
81
barcode.py
|
@ -1,78 +1,67 @@
|
||||||
from pylibdmtx import pylibdmtx as dmtx
|
from pylibdmtx import pylibdmtx as dmtx
|
||||||
from PIL import Image, ImageDraw, ImageOps
|
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)
|
white = (255, 255, 255)
|
||||||
black = (0, 0, 0)
|
black = (0, 0, 0)
|
||||||
|
|
||||||
nullimg = Image.new('RGB', (9, 15), white)
|
nullimg = Image.new('RGB', (9, 15), white)
|
||||||
|
|
||||||
|
chars = [0] * 10
|
||||||
|
|
||||||
# Draw zero
|
# Draw zero
|
||||||
zero = nullimg.copy()
|
chars[0] = nullimg.copy()
|
||||||
draw = ImageDraw.Draw(zero)
|
draw = ImageDraw.Draw(chars[0])
|
||||||
draw.rectangle([(0, 0), (8, 14)], fill=white, outline=black, width=3)
|
draw.rectangle([(0, 0), (8, 14)], fill=white, outline=black, width=3)
|
||||||
|
|
||||||
# Draw one
|
# Draw one
|
||||||
one = nullimg.copy()
|
chars[1] = nullimg.copy()
|
||||||
draw = ImageDraw.Draw(one)
|
draw = ImageDraw.Draw(chars[1])
|
||||||
draw.rectangle([(3, 0), (5, 14)], fill=black, outline=black, width=3)
|
draw.rectangle([(3, 0), (5, 14)], fill=black, outline=black, width=3)
|
||||||
|
|
||||||
# Draw eight
|
# Draw eight
|
||||||
eight = zero.copy()
|
chars[8] = chars[0].copy()
|
||||||
draw = ImageDraw.Draw(eight)
|
draw = ImageDraw.Draw(chars[8])
|
||||||
draw.rectangle([(3, 6), (5, 8)], fill=black, outline=black, width=3)
|
draw.rectangle([(3, 6), (5, 8)], fill=black, outline=black, width=3)
|
||||||
|
|
||||||
# Draw two and three
|
# Draw two and three
|
||||||
two = eight.copy()
|
chars[2] = chars[8].copy()
|
||||||
draw = ImageDraw.Draw(two)
|
draw = ImageDraw.Draw(chars[2])
|
||||||
draw.rectangle([(0, 3), (2, 5)], fill=white, outline=white, width=3)
|
draw.rectangle([(0, 3), (2, 5)], fill=white, outline=white, width=3)
|
||||||
three = two.copy() # copy to three
|
chars[3] = chars[2].copy() # copy to three
|
||||||
draw.rectangle([(6, 9), (8, 11)], fill=white, outline=white, width=3)
|
draw.rectangle([(6, 9), (8, 11)], fill=white, outline=white, width=3)
|
||||||
# Finish three
|
# Finish three
|
||||||
draw = ImageDraw.Draw(three)
|
draw = ImageDraw.Draw(chars[3])
|
||||||
draw.rectangle([(0, 9), (2, 11)], fill=white, outline=white, width=3)
|
draw.rectangle([(0, 9), (2, 11)], fill=white, outline=white, width=3)
|
||||||
|
|
||||||
# Draw nine
|
# Draw nine
|
||||||
nine = eight.copy()
|
chars[9] = chars[8].copy()
|
||||||
draw = ImageDraw.Draw(nine)
|
draw = ImageDraw.Draw(chars[9])
|
||||||
draw.rectangle([(0, 9), (5, 14)], fill=white, outline=white, width=3)
|
draw.rectangle([(0, 9), (5, 14)], fill=white, outline=white, width=3)
|
||||||
|
|
||||||
# draw four
|
# draw four
|
||||||
four = nine.copy()
|
chars[4] = chars[9].copy()
|
||||||
draw = ImageDraw.Draw(four)
|
draw = ImageDraw.Draw(chars[4])
|
||||||
draw.rectangle([(3, 0), (5, 2)], fill=white, outline=white, width=3)
|
draw.rectangle([(3, 0), (5, 2)], fill=white, outline=white, width=3)
|
||||||
|
|
||||||
# draw five
|
# draw five
|
||||||
five = eight.copy()
|
chars[5] = chars[8].copy()
|
||||||
draw = ImageDraw.Draw(five)
|
draw = ImageDraw.Draw(chars[5])
|
||||||
draw.rectangle([(6, 3), (8, 5)], fill=white, outline=white, width=3)
|
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.rectangle([(0, 9), (2, 11)], fill=white, outline=white, width=3)
|
||||||
|
|
||||||
# draw six
|
# draw six
|
||||||
six = eight.copy()
|
chars[6] = chars[8].copy()
|
||||||
draw = ImageDraw.Draw(six)
|
draw = ImageDraw.Draw(chars[6])
|
||||||
draw.rectangle([(3, 0), (8, 5)], fill=white, outline=white, width=3)
|
draw.rectangle([(3, 0), (8, 5)], fill=white, outline=white, width=3)
|
||||||
|
|
||||||
# draw seven
|
# draw seven
|
||||||
seven = eight.copy()
|
chars[7] = chars[8].copy()
|
||||||
draw = ImageDraw.Draw(seven)
|
draw = ImageDraw.Draw(chars[7])
|
||||||
draw.rectangle([(0, 3), (5, 14)], fill=white, outline=white, width=3)
|
draw.rectangle([(0, 3), (5, 14)], fill=white, outline=white, width=3)
|
||||||
|
|
||||||
tgt = 'cmdsite/polls/static/polls/'
|
tgt = 'cmdsite/polls/static/polls/'
|
||||||
|
|
||||||
# create 'font'
|
|
||||||
chars = [zero, one, two, three, four, five, six, seven, eight, nine]
|
|
||||||
|
|
||||||
def dmxcreate(value):
|
def dmxcreate(value):
|
||||||
val = str(value)
|
val = str(int(value))
|
||||||
# create value image
|
# create value image
|
||||||
valimg = Image.new('RGB',
|
valimg = Image.new('RGB',
|
||||||
(((9 * len(val)) + (3 * (len(val) - 1))), 15),
|
(((9 * len(val)) + (3 * (len(val) - 1))), 15),
|
||||||
|
@ -92,25 +81,9 @@ def dmxcreate(value):
|
||||||
dmximg.height + valimg.height),
|
dmximg.height + valimg.height),
|
||||||
white)
|
white)
|
||||||
img.paste(dmximg, (round(abs((dmximg.width - img.width) / 2)), 0))
|
img.paste(dmximg, (round(abs((dmximg.width - img.width) / 2)), 0))
|
||||||
# img.paste(dmximg)
|
|
||||||
img.paste(valimg, (round(abs((valimg.width - img.width) / 2)),
|
img.paste(valimg, (round(abs((valimg.width - img.width) / 2)),
|
||||||
dmximg.height))
|
dmximg.height))
|
||||||
# img.paste(valimg, (0, dmximg.height))
|
|
||||||
return img
|
# Return grayscale image with transparency
|
||||||
|
img.putalpha(ImageOps.invert(img).convert('L'))
|
||||||
# combine images
|
return img.convert('L')
|
||||||
#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