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

Douglas Bagnall douglas at paradise.net.nz
Sun Jun 19 10:37:01 CEST 2005


hi Peter,

> 
> Thanks for the reply folks. Here is the code I have produced so far...
> #! /usr/bin/python
> 
> import sys
> import string

Don't use the string module, use the string type methods:

http://python.org/doc/2.3.5/lib/string-methods.html

Read the next page too, about string formatting.  You'll find your 
helper functions are reducible to one-liners:

def hexit(n):		
     return "%.2X" % ord(n)

def hex2dec(n):		
     return int(n, 16)

def invhex(n):
     return "%.2X" % (255 - ord(n))

It is also inadvisable to use "+" to build up a long string -- better to 
  use a list, then join it up with ''.join().  Also, your code mixes 
tabs and spaces, which only leads to trouble in the long run.  It ought 
to be possible to make you text editor stick to one or the other.

Nevertheless, the main problem seems to be that you are ignoring PIL and 
parsing the image files yourself.  I would suggest something like this:

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