[Image-SIG] Converting to zebra (ZPL) format

Peter Dempsey yarglags at eircom.net
Tue Jun 21 01:07:49 CEST 2005

Thanks Douglas and Jaos for your replies.

I made some progress today by using PIL today. I managed to load the image and 
cycle through the raw image data. The code is below. I'll look at your 
suggestion tomorrow, Douglas. The string types methods sound a bit more 
useful than my fumblings. As I said, I'm a newbie. About the spaces mixed 
with tabs, I'm using GVIM to edit the code but I'm one of those people who 
over-formats code. I like it to look orderly using whatever means comes to 

My code produces a string of 1s and zeros corresponding to the image. I'll 
work out a way of converting it to ascii-hex tomorrow. The old code was an 
attempt to de-compress pcx data but I don't need to do that if I use PIL. If 
I convert the image to mono I should be able to use any source image.

Ideally, I should be able to produce a decoder plug-in for ZPL but I have a 
lot to learn before that happens. Given the lack of hits I got while looking 
for a ready-made converter, there is not much demand for such a plug-in. Ah 
well, if it get it working I'll make it available and then work on others for 
DPL, Datamax Programming Language and BPL, Brady Programming Language. I may 
need one for Citizen ticket printers also.

I'll stop rambling, here's my code.

Thanks folks,


#! /usr/bin/env python

import Image, sys, string

def hexit(n):		# convert n to two digit Hex string.
	result = string.upper(str(hex(ord(n))))[2:4]
	if len(result) == 1:
		result = '0' + result
	return result

def ImageWidth(n):	# read image width from PCX header
	if n % 8 > 0:	# if mod width non zero image needs extra byte
		n = n / 8 + 1
		n = n / 8
	return n

def ImageHeight(n):	# read height from PCX header
	return 1 + (ord(n[10]) + ord(n[11]) * 256) - (ord(n[6]) + ord(n[7]) * 256)

def PrintDetails(n):
	print 'Input length:\t', len(n)
	print 'Image Width: \t', ImageWidth(n), ' bytes'

def hex2dec(n):		# convert hex digit to decimal
	return string.find('0123456789ABCDEF',n)

def invhex(n):		# forgot that printer treats 1 as 'on' - no ink dot.
	n = string.upper(hex(255 - hex2dec(n[0])*16 - hex2dec(n[1]))[2:])
	if len(n) == 2:
		return n
		return '0'+n

i = sys.argv[1:]

for arg in i:
        source = Image.open(arg)
    except IOError:
        print 'cannot open', arg
	dest = arg
	dest = dest[:string.find(dest,'.')]+'.hex'
        print 'Input file:\t\t', i[0]
        print 'Output file:\t\t', dest
	print 'Image Width  (px):\t', width,' px'
	print 'Image Height (px):\t', height,' px'
        print 'Image Mode: \t\t', source.mode
	print 'HexWidth:\t\t',hexwidth
	header = '~DG_ZEBRA,' + str(hexwidth*height) + ',' + str(hexwidth) + ',\n'
	print header
	n = ''
	for y in range(height):
		for x in range(width):
			pixel= source.getpixel((x,y))
			if pixel==255:
			n=n+ str(pixel)
	print n

On Sunday 19 Jun 2005 09:37, Douglas Bagnall wrote:
> hi Peter,
> > Thanks for the reply folks. Here is the code I have produced so far...
> > #! /usr/bin/python
> >
> > import sys
> > import string

More information about the Image-SIG mailing list