print-labels-gui/barcodegen.py

57 lines
2.0 KiB
Python
Executable File

import sys,os
from barcode import EAN13
from barcode.writer import ImageWriter
from barcode.errors import NumberOfDigitsError, IllegalCharacterError
from PIL import Image, ImageOps, ImageDraw, ImageFont, ImageColor
from PIL.BdfFontFile import BdfFontFile
class ImageWitoutTextWriter(ImageWriter):
def _paint_text(self, xpos, ypos):
pass
def create_code_png(n, code, text):
my_code = EAN13(code, writer=ImageWitoutTextWriter())
my_code.save("code" + str(n), {"module_height":11.0,"module_width":0.3,"dpi":169, "font_size":0, "text_distance":1})
image_file = Image.open("code{}.png".format(str(n)))
image_file = image_file.convert('1', dither=Image.Dither.NONE)
img=ImageOps.expand(image_file, border=17, fill=1)
draw = ImageDraw.Draw(img)
dst = "terminus.pil"
#with open("/home/applekeeper_cat/Загрузки/Terminus/ter-u16n.bdf", "rb") as f:
# bdf = BdfFontFile(f)
# bdf.save(dst)
font = ImageFont.load(dst)
draw.text((10,3), text, fill="black", font=font)
draw.text((100,100), my_code.get_fullcode(), fill="black", font=font)
img.save('code{}.png'.format(str(n)))
code = sys.argv[1]
try:
text = sys.argv[2]
except:
pass
n = 1
if(code == "i"):
while(True):
try:
code = input("Код (q для печати/выхода): ")
if(code == "q"):
com_str = "ptouch-print "
for i in range(1, n):
com_str += "--image \"code" + str(i) + ".png\" --pad 5 "
os.system(com_str)
os.system("rm -f code*.png")
break
text = input("Текст: ")
create_code_png(n, code, text)
n+=1
except (NumberOfDigitsError, IllegalCharacterError):
print("Код введен неправильно.")
else:
create_code_png(1, code, text)
os.system("ptouch-print --image \"code1.png\"")
os.system("rm -f code1.png")