[Tutor] greyscale bitmaps with python

John Fusco fusco_john at yahoo.com
Sat Jul 10 01:27:55 CEST 2004


Thanks for the tips, but neither passed the "quick and
dirty" test.

The xbitmap is monochrome so that won't work (too
dirty).

PIL works, but I'm stuck with Python 1.5.2 and I need
full source to build for Python 1.5.2 (not quick
enough).

I finally settled on the pattern below, which creates
a PGM file and uses 'ee' to display.

Regards,
John


import os

def pgmhdr(rows,cols,depth):
	return "P2\n%d %d\n%d\n" % (cols,rows,depth)

class PgmImage:
	def __init__(self,num_rows,num_cols,depth,pixels):

		assert(num_rows*num_cols==len(pixels))
		self.num_rows = num_rows
		self.num_cols = num_cols
		self.depth    = depth
		self.pixels   = pixels

		self.fmt = ("%d " * num_cols + "\n") * num_rows

	def show(self):
		fn = "/tmp/foo.pgm"
		file = open(fn,"wb")
		print file
		file.write(self.pgmdata())
		file.close()
		os.system("ee " + fn)

	def pgmdata(self):
		return pgmhdr(num_rows,num_cols,256) + self.fmt %
tuple(self.pixels)

if __name__ == "__main__":
	num_rows=256
	num_cols=256
	pixels = range(num_rows)*num_cols

	PgmImage(num_rows,num_cols,256,pixels).show()



		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 


More information about the Tutor mailing list