467,859 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,859 developers. It's quick & easy.

Cellular automata and image manipulation

Hello. I have recently been experimenting with cellular automata and I
would like to know how I could convert a 2d list of 0's and 1's into
white and black squares on an image. I have tried to install matplotlib
and also NumTut but both to no avail. There seem to be bugs in their
installation and I have not been able to figure out how to resolve
them. I would be happy for someone to suggest a library and maybe give
a simple example of how to do what I want to do.

May 13 '06 #1
  • viewed: 2627
Share:
7 Replies
de*****@gmail.com wrote:
Hello. I have recently been experimenting with cellular automata and I
would like to know how I could convert a 2d list of 0's and 1's into
white and black squares on an image. I have tried to install matplotlib
and also NumTut but both to no avail. There seem to be bugs in their
installation and I have not been able to figure out how to resolve
them. I would be happy for someone to suggest a library and maybe give
a simple example of how to do what I want to do.


Why don't you explain your problems with installing numpy (the Numeric Tutorial
is out of date; don't bother with NumTut) or matplotlib on the appropriate
mailing lists?

http://lists.sourceforge.net/lists/l...mpy-discussion
http://lists.sourceforge.net/lists/l...tplotlib-users

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 13 '06 #2

<de*****@gmail.com> wrote in message
news:11**********************@d71g2000cwd.googlegr oups.com...
Hello. I have recently been experimenting with cellular automata and I
would like to know how I could convert a 2d list of 0's and 1's into
white and black squares on an image. I have tried to install matplotlib
and also NumTut but both to no avail. There seem to be bugs in their
installation and I have not been able to figure out how to resolve
them. I would be happy for someone to suggest a library and maybe give
a simple example of how to do what I want to do.

In my 1d cellular automaton, I used the python image library and
import Image
nim = Image.new("1", (height * 2, height))
nim.putdata(bimg)
nim.resize((400,200)).save("output.png")

where bimg is a 2d list of 0's and 1's. You could probably remove the
resize.
May 14 '06 #3
Thank you very much. That is highly simple, useful and it works.

May 14 '06 #4
It seemed to work with a 1d list but not with 2d.

May 14 '06 #5
import Image
x = []
buff = []

buff = [[0 for y in range(41)] for x in range(21)]
buff[0][(len(buff)-1)/2] = 1
def rule1():
for i in range(len(buff)-1):
for j in range(len(buff[0])-1):
if i == len(buff)-1:
break
elif j == 0:
if buff[i][j+1] == 1:
buff[i+1][j] = 1
elif j == len(buff[0])-1:
if buff[i][j-1] == 1:
buff[i+1][j] = 1
elif buff[i][j-1] == 1:
buff[i+1][j] = 1
elif buff[i][j+1] == 1:
buff[i+1][j] = 1

def rule2():
for i in range(len(buff)-1):
for j in range(len(buff[0])-1):
if i == len(buff)-1:
break
elif j == 0:
if buff[i][j+1] == 1:
buff[i+1][j] = 1
elif j == len(buff[0])-1:
buff[i+1][j] = 1
elif buff[i][j-1] == 1 and buff[i][j+1] != 1:
buff[i+1][j] = 1
elif buff[i][j+1] == 1 and buff[i][j-1] != 1:
buff[i+1][j] = 1
rule2()
nim = Image.new("1", (400,600))
nim.putdata(buff)
nim.resize((400,600)).save("output.png")

for a in buff:
for x in a:
if x == 1:
print "X",
else: print " ",
print ""

That is my code. Could you tell me what maybe is wrong? Rule2 makes the
fibonacci triangle btw.

May 14 '06 #6
Sorry this is the latest, the previous didn't work so well:

import Image
x = []
buff = []

buff = [[0 for y in range(41)] for x in range(21)]
buff[0][(len(buff[0])-1)/2] = 1
def rule1():
for i in range(len(buff)-1):
for j in range(len(buff[0])-1):
if i == len(buff)-1:
break
elif j == 0:
if buff[i][j+1] == 1:
buff[i+1][j] = 1
elif j == len(buff[0])-1:
if buff[i][j-1] == 1:
buff[i+1][j] = 1
elif buff[i][j-1] == 1:
buff[i+1][j] = 1
elif buff[i][j+1] == 1:
buff[i+1][j] = 1

def rule2():
for i in range(len(buff)-1):
for j in range(len(buff[0])-1):
if i == len(buff)-1:
break
elif j == 0:
if buff[i][j+1] == 1:
buff[i+1][j] = 1
elif j == len(buff[0])-1:
buff[i+1][j] = 1
elif buff[i][j-1] == 1 and buff[i][j+1] != 1:
buff[i+1][j] = 1
elif buff[i][j+1] == 1 and buff[i][j-1] != 1:
buff[i+1][j] = 1
elif buff[i][len(buff[0])-1] == 1 or buff[i][0] == 1:
break
rule2()
nim = Image.new("1", (50,50))
nim.putdata(buff)
nim.resize((400,600)).save("output.png")

for a in buff:
for x in a:
if x == 1:
print "X",
elif x == 0: print " ",
print ""

for a in buff:
print a

May 14 '06 #7
Actually never mind either. I guessed I needed to append all values
after eachother in one row list:
x = []
for y in buff:
for z in y:
x.append(z)

thanks for the help

May 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rune Johansen | last post: by
reply views Thread by XMLGuy | last post: by
9 posts views Thread by Job | last post: by
9 posts views Thread by zacariaz | last post: by
10 posts views Thread by Pulzar | last post: by
3 posts views Thread by jon | last post: by
8 posts views Thread by shotokan99 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.