[Image-SIG] Converting to zebra (ZPL) format
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.
#! /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
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
def ImageHeight(n): # read height from PCX header
return 1 + (ord(n) + ord(n) * 256) - (ord(n) + ord(n) * 256)
print 'Input length:\t', len(n)
print 'Image Width: \t', ImageWidth(n), ' bytes'
def hex2dec(n): # convert hex digit to decimal
def invhex(n): # forgot that printer treats 1 as 'on' - no ink dot.
n = string.upper(hex(255 - hex2dec(n)*16 - hex2dec(n))[2:])
if len(n) == 2:
i = sys.argv[1:]
for arg in i:
source = Image.open(arg)
print 'cannot open', arg
dest = arg
dest = dest[:string.find(dest,'.')]+'.hex'
print 'Input file:\t\t', i
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
header = '~DG_ZEBRA,' + str(hexwidth*height) + ',' + str(hexwidth) + ',\n'
n = ''
for y in range(height):
for x in range(width):
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