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

Peter Dempsey yarglags at eircom.net
Wed Jun 22 04:40:19 CEST 2005

Hi Douglas,

Your solution worked very well with a couple of minor tweaks. 'Takes a lot 
less lines of code than my effort. This is what I came up with today...

#! /usr/bin/env python

import Image, sys

for arg in sys.argv[1:]:
	im = Image.open(arg).convert("1").rotate(0)	# convert("1") means can use 
										# colour images

	data = im.tostring("raw", "1;I")
	size = len(data)
	data = ["%02X" % ord(byte) for byte in data]
	name = arg[:arg.rfind('.')][:8]+'.GRF' # name can be '8.3' defaults to '.GRF'

	print "~DG%s,%d,%d," % (name,size, (im.size[0]+7)/8) # no need for the '^m'
	print "".join(data)
	print "^FO  40,  40 ^IM%s  ^FS" % (name)	# Print graphic at 40,40

I rarely need to do more than one image at a time so I think I'll play around 
with arguments for rotation and scaling. I was making some progress with my 
previous version but Bog knows what my loops on the data would have turned 

ZPL image data can be compressed using the scheme described in the pdf manual. 
I might look into this once I figure out how this code works.

I have used a '+' in my code. I have not got the hang of  ''.join() yet. The 
three functions you described were much simpler than mine. I have a lot to 

Thanks very much for you help,


On Sunday 19 Jun 2005 09:37, Douglas Bagnall wrote:
> hi Peter,
> def hexit(n):
>      return "%.2X" % ord(n)
> def hex2dec(n):
>      return int(n, 16)
> def invhex(n):
>      return "%.2X" % (255 - ord(n))
> import sys
> import Image
> for arg in sys.argv[1:]:
>      im = Image.open(arg)
> #this is equivalent to step 1 of Fredrik's reply. Step 2 went like this:
>      data = im.tostring("raw", "1;I")
>      size = len(data)
>      data = ["%02X" % ord(byte) for byte in data]
>      print "%d,%d^m" % (size, (im.size[0]+7)/8)
>      print "".join(data)
> try it.
> douglas

More information about the Image-SIG mailing list