Skip to content

Instantly share code, notes, and snippets.

@Lukse
Last active August 21, 2016 08:40
Show Gist options
  • Save Lukse/a8afa42219fe8cf641f3c7b8c60e9338 to your computer and use it in GitHub Desktop.
Save Lukse/a8afa42219fe8cf641f3c7b8c60e9338 to your computer and use it in GitHub Desktop.
Generate barcode
from StringIO import StringIO
from PIL import Image
import cv2
import barcode
from barcode.writer import ImageWriter
import numpy as np
# print barcode.PROVIDED_BARCODES
code = barcode.get('code128', 'RN012345678LT', writer=ImageWriter())
options = {}
options['module_width'] = 1
options['module_height'] = 40.0
options['center_text'] = True
options['font_size'] = 70
options['text_distance'] = 3
fp = StringIO()
code.write(fp, options)
fp.seek(0)
img_array = np.asarray(bytearray(fp.read()), dtype=np.uint8)
img = cv2.imdecode(img_array, 0)
cv2.imshow("im", img)
cv2.imwrite("code.png", img)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment