|
osdir.com mailing list archive |
|
Subject: Re: Converting to zebra (ZPL) format - msg#00041List: python.imageI 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 hand. 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, Peter #! /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 else: 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 else: return '0'+n i = sys.argv[1:] for arg in i: try: source = Image.open(arg) except IOError: print 'cannot open', arg else: dest = arg dest = dest[:string.find(dest,'.')]+'.hex' print 'Input file:\t\t', i[0] print 'Output file:\t\t', dest width=source.size[0] print 'Image Width (px):\t', width,' px' height=source.size[1] print 'Image Height (px):\t', height,' px' print 'Image Mode: \t\t', source.mode hexwidth=ImageWidth(width) 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: pixel=1 n=n+ str(pixel) print n del(source) #f=file(dest,"w") #f.write(n) #f.close() 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 <SNIP> _______________________________________________ Image-SIG maillist - Image-SIG@xxxxxxxxxx http://mail.python.org/mailman/listinfo/image-sig
Thread at a glance:
Previous Message by Date: (click to view message preview)Re: pil ten year anniversaryCongrats! In case you didn't know, PIL is heavily used in UpLib (see http://www.parc.com/janssen/pubs/TR-03-16.pdf and http://www.parc.com/janssen/pubs/TR-04-11.pdf). Great work, Fredrik! Bill _______________________________________________ Image-SIG maillist - Image-SIG@xxxxxxxxxx http://mail.python.org/mailman/listinfo/image-sig Next Message by Date: click to view message previewRe: Converting to zebra (ZPL) formatOn Monday 20 June 2005 20:07, Peter Dempsey wrote: > Thanks Douglas and Jaos for your replies. > 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. > If you are diving into writing code for these pritners, you mguiht whant to get in touch with people at the Linux Printing project, so that yu code may become apropriate drivers for these devices in the end. Maybe they will further point you to either gutenprint (ex gimp-print) or Ghostscript devel. either way, it'd mean that these printers would be supported "out of the box" for millions of GNU/Linux (and other systems) worldwide. Regards, JS -><- _______________________________________________ Image-SIG maillist - Image-SIG@xxxxxxxxxx http://mail.python.org/mailman/listinfo/image-sig Previous Message by Thread: click to view message previewRe: Converting to zebra (ZPL) formathi 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 _______________________________________________ Image-SIG maillist - Image-SIG@xxxxxxxxxx http://mail.python.org/mailman/listinfo/image-sig Next Message by Thread: click to view message previewRe: Converting to zebra (ZPL) formatOn Monday 20 June 2005 20:07, Peter Dempsey wrote: > Thanks Douglas and Jaos for your replies. > 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. > If you are diving into writing code for these pritners, you mguiht whant to get in touch with people at the Linux Printing project, so that yu code may become apropriate drivers for these devices in the end. Maybe they will further point you to either gutenprint (ex gimp-print) or Ghostscript devel. either way, it'd mean that these printers would be supported "out of the box" for millions of GNU/Linux (and other systems) worldwide. Regards, JS -><- _______________________________________________ Image-SIG maillist - Image-SIG@xxxxxxxxxx http://mail.python.org/mailman/listinfo/image-sig
Web Hosting Reviews from OSDir.com Sister Site iBizWebHosting.com
|
|