This paste expires on 2023-04-13 10:18:34.261024. Repaste, or download this paste. . Pasted through v1-api.

#!/usr/bin/python
# vim: noet nowrap number ts=4
import barcode
#import bytes_as_braille as bab
class MakeAnInt(Exception): pass
barcode.Code128.default_writer_options = {
"module_width": 0.2,
"module_height": 10.0,
"quiet_zone": 6.5,
"font_size": 10,
"text_distance": 3.0,
"background": None,
"foreground": "black",
"write_text": True,
"text": "",
}
def mkBar( byte_content, ASCII = True ):
if not ASCII:
string_content = str(int.from_bytes(byte_content,'big'))
else:
try:
try:
int(byte_content)
raise MakeAnInt
except ValueError:
string_content =  byte_content.decode('ascii')
except (UnicodeDecodeError, MakeAnInt,):
string_content = str(int.from_bytes(byte_content,'big'))
print(f"Encoded: {string_content}")
b = barcode.Code128( string_content )
b.write(open('/tmp/barcode.svg','wb'), text = byte_content.decode('utf-8'))
if __name__ == '__main__':
while True:
#mkBar( bab.input() )
mkBar( input('> ').encode('utf-8') )
## all good
#mkBar(b'gdgdggdgd')
#mkBar('ddddddé'.encode('utf-8'))
## why is "converting to int" not being displayed?!?!" that except clause is not being used!
#mkBar(b'12345689')
Filename: mk_barcodes.py. Size: 1kb. View raw, , hex, or download this file.