Skip to content

Instantly share code, notes, and snippets.

@scruss
Last active July 28, 2025 02:14
Show Gist options
  • Select an option

  • Save scruss/36c31a9b653779b2b7d1 to your computer and use it in GitHub Desktop.

Select an option

Save scruss/36c31a9b653779b2b7d1 to your computer and use it in GitHub Desktop.

Revisions

  1. scruss revised this gist Jan 10, 2021. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions esc-pos-image.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    #!/usr/bin/python
    #!/usr/bin/env python3
    # esc-pos-image.py - print image files given as command line arguments
    # to simple ESC-POS image on stdout
    # scruss - 2014-07-26 - WTFPL (srsly)
    # - 2021-01-09 (made python3 compatible - tvm Carko!)

    # if you want a proper CUPS driver for a 58mm thermal printer
    # that uses this command set, go here:
    @@ -13,23 +14,23 @@
    import struct

    # give usage and exit if no arguments
    if len(sys.argv) == 1:
    print 'Usage:', sys.argv[0], \
    'image1 image2 ... [ > printer_device ]'
    if (len(sys.argv) == 1):
    print('Usage:', sys.argv[0],
    'image1 image2 ... [ > printer_device ]')
    exit(1)

    # print all of the images!
    for i in sys.argv[1:]:
    im = Image.open(i)

    # if image is not 1-bit, convert it
    if im.mode != '1':
    if (im.mode != '1'):
    im = im.convert('1')

    # if image width is not a multiple of 8 pixels, fix that
    if im.size[0] % 8:
    if (im.size[0] % 8):
    im2 = Image.new('1', (im.size[0] + 8 - im.size[0] % 8,
    im.size[1]), 'white')
    im.size[1]), 'white')
    im2.paste(im, (0, 0))
    im = im2

    @@ -40,9 +41,9 @@
    im = im.convert('1')

    # output header (GS v 0 \000), width, height, image data
    sys.stdout.write(''.join(('\x1d\x76\x30\x00',
    struct.pack('2B', im.size[0] / 8 % 256,
    im.size[0] / 8 / 256),
    struct.pack('2B', im.size[1] % 256,
    im.size[1] / 256),
    im.tobytes())))
    sys.stdout.buffer.write(b''.join((bytearray(b'\x1d\x76\x30\x00'),
    struct.pack('2B', int(im.size[0] / 8) % 256,
    int(im.size[0] / (8 * 256))),
    struct.pack('2B', im.size[1] % 256,
    int(im.size[1] / 256)),
    im.tobytes())))
  2. scruss revised this gist Jul 12, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions esc-pos-image.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,10 @@
    # to simple ESC-POS image on stdout
    # scruss - 2014-07-26 - WTFPL (srsly)

    # if you want a proper CUPS driver for a 58mm thermal printer
    # that uses this command set, go here:
    # https://github.com/klirichek/zj-58

    import sys
    from PIL import Image
    import PIL.ImageOps
  3. scruss revised this gist Jul 27, 2014. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions esc-pos-image.py
    Original file line number Diff line number Diff line change
    @@ -36,6 +36,9 @@
    im = im.convert('1')

    # output header (GS v 0 \000), width, height, image data
    print ''.join(('\x1d\x76\x30\x00', struct.pack('2B', im.size[0] / 8
    % 256, im.size[0] / 8 / 256), struct.pack('2B',
    im.size[1] % 256, im.size[1] / 256), im.tobytes()))
    sys.stdout.write(''.join(('\x1d\x76\x30\x00',
    struct.pack('2B', im.size[0] / 8 % 256,
    im.size[0] / 8 / 256),
    struct.pack('2B', im.size[1] % 256,
    im.size[1] / 256),
    im.tobytes())))
  4. scruss created this gist Jul 26, 2014.
    41 changes: 41 additions & 0 deletions esc-pos-image.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/usr/bin/python
    # esc-pos-image.py - print image files given as command line arguments
    # to simple ESC-POS image on stdout
    # scruss - 2014-07-26 - WTFPL (srsly)

    import sys
    from PIL import Image
    import PIL.ImageOps
    import struct

    # give usage and exit if no arguments
    if len(sys.argv) == 1:
    print 'Usage:', sys.argv[0], \
    'image1 image2 ... [ > printer_device ]'
    exit(1)

    # print all of the images!
    for i in sys.argv[1:]:
    im = Image.open(i)

    # if image is not 1-bit, convert it
    if im.mode != '1':
    im = im.convert('1')

    # if image width is not a multiple of 8 pixels, fix that
    if im.size[0] % 8:
    im2 = Image.new('1', (im.size[0] + 8 - im.size[0] % 8,
    im.size[1]), 'white')
    im2.paste(im, (0, 0))
    im = im2

    # Invert image, via greyscale for compatibility
    # (no, I don't know why I need to do this)
    im = PIL.ImageOps.invert(im.convert('L'))
    # ... and now convert back to single bit
    im = im.convert('1')

    # output header (GS v 0 \000), width, height, image data
    print ''.join(('\x1d\x76\x30\x00', struct.pack('2B', im.size[0] / 8
    % 256, im.size[0] / 8 / 256), struct.pack('2B',
    im.size[1] % 256, im.size[1] / 256), im.tobytes()))