all i want to do is plot a matrix!!!

Huaiyu Zhu hzhu at users.sourceforge.net
Wed Oct 18 14:19:10 EDT 2000


On Tue, 17 Oct 2000 09:39:32 +0100, taz <tariq.rashid at iname.com> wrote:
>
>i've looked in DISLIN, numpy, Pthyon imaging library...
>
>all i want to do is import an image (say .png, .gif) to a matrix of colour
>values (rgb) so i can manipulate them with my own maths functions, do
>lookups etc etc ...
>
>and be able to plot the images in a window...

Here's a program I've played with, which displays an image of mandelbrot set
with a faked rgb colormap, using the view function in NumTut (comes with
Numeric).  There may be better alternatives.  Please let me know if you find
one.

#!/usr/bin/env python
# view Mandelbrot set. Distribute freely.
# 
# Rob Hooft, 1996. ASCII-art using Numeric Python 1.0beta1
# Tim Lavoie, 2000. Additional kluges and hackery, b/w view.
# Huaiyu Zhu, 2000. Change to class, rgb view, some ckeanup and speedup.

from Numeric import arange, NewAxis, zeros, where, greater, \
                    tanh, array, transpose
from NumTut import view
from time import time, sleep

def color(x,a,b,c):
	"""
	Hacked rgb color map.  Three control parameters, roughly:
	a is green,	b is red, c is blue
	"""
	r = (tanh(-a*(x-b))+1)/2
	b = (tanh(a*(x-c))+1)/2
	g = 1-r-b
	return r, g, b

def viewrgb(rgb, wtime=None):
	""" view rgb
	"""
	c = array(rgb)
	c = transpose(c,(2,1,0))
	print c.shape
	view(c)
	wait(wtime)

def wait(wtime):
	if wtime is None:	input("Press Enter")
	else:		sleep(wtime)

class Mandelbrot:
	fraccode = 0
	def __init__(self, frac='z*z+c'):
		self._time = time()
		self.fraccode = compile(frac, '<string>', 'eval')

	def set_size(self, x0=-2.1, x1=0.7, y0=-1.2, y1=1.2, nx=20, ny=20):
		"""Set the bounding box (x0, y0, x1, y1) and resolutions (nx, ny)"""
		self.xx = arange(x0, x1, (x1-x0)/nx)
		self.yy = arange(y1, y0, (y0-y1)/ny)*1j
	
	def calc(self, c0, z0, maxiter=30):
		""" Calculating Mandelbrot set.  Initial value z
		Not updating c produces fancy bands outside the set
		result set to z.real is colored
		result set to finished is black and white"""
		print time() - self._time
		c = self.xx + self.yy[:,NewAxis]
		z = zeros(c.shape)
		finished = zeros(c.shape)
		print z.shape, c.shape, time() - self._time
	
		fraccode = self.fraccode
		for iter in range(maxiter):
			z = eval(fraccode)
			finished = greater(abs(z), 2)
			c = where(finished, c0, c) # external bands
			z = where(finished, z0, z)
		self.result = z.real
		#self.result = finished			# black and white
		print z.shape, c.shape, time() - self._time

	def view(self, a=3, b=.2, c=1.3):
		"View result in rgb color"
		viewrgb(color(self.result, a, b, c), wtime=0)
		print time() - self._time

if __name__ == "__main__":
	f = Mandelbrot()
	test_params = 1
	if test_params:
		f.set_size(nx=160, ny=160)
		for c0 in [0.0, 0.15, 0.25]:
			for z0 in [1.0, 1.1, 1.25, 1.4]:
				f.calc(c0, z0, maxiter=30)
				f.view()
	else:
		print "This will take a while ..."
		f.set_size(x0=-0.712, x1=-0.707, y0=0.267, y1=0.272, nx=512, ny=512)
		f.calc(0.15, 1.1, maxiter=190)
		f.view()
	
	raw_input("Press Enter ...")
	
	





More information about the Python-list mailing list