Skip to content

Instantly share code, notes, and snippets.

@fernandomv3
Last active June 20, 2017 19:06
Show Gist options
  • Save fernandomv3/fcce51068317f5fa5d58b18f72544d4f to your computer and use it in GitHub Desktop.
Save fernandomv3/fcce51068317f5fa5d58b18f72544d4f to your computer and use it in GitHub Desktop.
Generate Code39 Barcodes with Python3 & Pillow
#!/usr/bin/env python
from PIL import Image, ImageFont, ImageDraw
def code39(filepath=".", text="", size=200, orientation="horizontal", printText=False, sizeFactor=8):
code_dict = {"0":"111221211","1":"211211112","2":"112211112","3":"212211111","4":"111221112","5":"211221111","6":"112221111","7":"111211212","8":"211211211","9":"112211211","A":"211112112","B":"112112112","C":"212112111","D":"111122112","E":"211122111","F":"112122111","G":"111112212","H":"211112211","I":"112112211","J":"111122211","K":"211111122","L":"112111122","M":"212111121","N":"111121122","O":"211121121","P":"112121121","Q":"111111222","R":"211111221","S":"112111221","T":"111121221","U":"221111112","V":"122111112","W":"222111111","X":"121121112","Y":"221121111","Z":"122121111","-":"121111212",".":"221111211"," ":"122111211","$":"121212111","/":"121211121","+":"121112121","%":"111212121","*":"121121211"}
upper_text = text.upper()
code_string = ''.join([code_dict[c] + "1" for c in upper_text])
code_string = "1211212111" + code_string + "121121211"
padding = 5
code_length = padding * 2
font = ImageFont.truetype("arial.ttf", 12 * sizeFactor)
text_height = font.getsize(upper_text)[1] + 5 if printText else 0
code_length += sum([int(e) for e in code_string])
width = 0
height = 0
if(orientation == "horizontal"):
width = code_length * sizeFactor
height = size
else:
height = code_length * sizeFactor
width = size
im = Image.new("L",(width,height + text_height),255)
draw = ImageDraw.Draw(im)
if(printText):
font = ImageFont.truetype("Arial.ttf", 12 * sizeFactor)
textPosition = (width - font.getsize(upper_text)[0]) // 2
draw.text((textPosition,height),upper_text,0,font)
loc = padding
for i,e in enumerate([int(e) for e in code_string]):
cur_size = loc + e
if(orientation == "horizontal"):
draw.rectangle([loc*sizeFactor,0,cur_size*sizeFactor,height], 0 if (i %2 == 0) else 255)
else:
draw.rectangle([0,loc*sizeFactor,width,cur_size*sizeFactor], 0 if (i %2 == 0) else 255)
loc = cur_size
del(draw)
im = im.rotate(90,expand=True).resize(((height+text_height),width),Image.LANCZOS)
im.save(filepath + "/" + text +".png")
def main():
l = ['47059016','00000000']
for code in l:
code39(filepath=".", text=code, size=260, orientation="horizontal", printText=True, sizeFactor=7)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment