cleanup datamatrix generator
This commit is contained in:
parent
61d9714ca3
commit
f79ed45dd3
79
barcode.py
79
barcode.py
|
@ -1,78 +1,67 @@
|
|||
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)
|
||||
|
||||
chars = [0] * 10
|
||||
|
||||
# Draw zero
|
||||
zero = nullimg.copy()
|
||||
draw = ImageDraw.Draw(zero)
|
||||
chars[0] = nullimg.copy()
|
||||
draw = ImageDraw.Draw(chars[0])
|
||||
draw.rectangle([(0, 0), (8, 14)], fill=white, outline=black, width=3)
|
||||
|
||||
# Draw one
|
||||
one = nullimg.copy()
|
||||
draw = ImageDraw.Draw(one)
|
||||
chars[1] = nullimg.copy()
|
||||
draw = ImageDraw.Draw(chars[1])
|
||||
draw.rectangle([(3, 0), (5, 14)], fill=black, outline=black, width=3)
|
||||
|
||||
# Draw eight
|
||||
eight = zero.copy()
|
||||
draw = ImageDraw.Draw(eight)
|
||||
chars[8] = chars[0].copy()
|
||||
draw = ImageDraw.Draw(chars[8])
|
||||
draw.rectangle([(3, 6), (5, 8)], fill=black, outline=black, width=3)
|
||||
|
||||
# Draw two and three
|
||||
two = eight.copy()
|
||||
draw = ImageDraw.Draw(two)
|
||||
chars[2] = chars[8].copy()
|
||||
draw = ImageDraw.Draw(chars[2])
|
||||
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)
|
||||
# Finish three
|
||||
draw = ImageDraw.Draw(three)
|
||||
draw = ImageDraw.Draw(chars[3])
|
||||
draw.rectangle([(0, 9), (2, 11)], fill=white, outline=white, width=3)
|
||||
|
||||
# Draw nine
|
||||
nine = eight.copy()
|
||||
draw = ImageDraw.Draw(nine)
|
||||
chars[9] = chars[8].copy()
|
||||
draw = ImageDraw.Draw(chars[9])
|
||||
draw.rectangle([(0, 9), (5, 14)], fill=white, outline=white, width=3)
|
||||
|
||||
# draw four
|
||||
four = nine.copy()
|
||||
draw = ImageDraw.Draw(four)
|
||||
chars[4] = chars[9].copy()
|
||||
draw = ImageDraw.Draw(chars[4])
|
||||
draw.rectangle([(3, 0), (5, 2)], fill=white, outline=white, width=3)
|
||||
|
||||
# draw five
|
||||
five = eight.copy()
|
||||
draw = ImageDraw.Draw(five)
|
||||
chars[5] = chars[8].copy()
|
||||
draw = ImageDraw.Draw(chars[5])
|
||||
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)
|
||||
chars[6] = chars[8].copy()
|
||||
draw = ImageDraw.Draw(chars[6])
|
||||
draw.rectangle([(3, 0), (8, 5)], fill=white, outline=white, width=3)
|
||||
|
||||
# draw seven
|
||||
seven = eight.copy()
|
||||
draw = ImageDraw.Draw(seven)
|
||||
chars[7] = chars[8].copy()
|
||||
draw = ImageDraw.Draw(chars[7])
|
||||
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)
|
||||
val = str(int(value))
|
||||
# create value image
|
||||
valimg = Image.new('RGB',
|
||||
(((9 * len(val)) + (3 * (len(val) - 1))), 15),
|
||||
|
@ -92,25 +81,9 @@ def dmxcreate(value):
|
|||
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)
|
||||
|
||||
# Return grayscale image with transparency
|
||||
img.putalpha(ImageOps.invert(img).convert('L'))
|
||||
return img.convert('L')
|
||||
|
|
Loading…
Reference in New Issue