This is a modified version of the previous article "Get complex results with short code-one-dimensional cellular automaton-". The following has been fixed.
--Implementing an automaton with a generator --Save the result as an image
When drawing with 1 cell and 1 pixel, [I don't know what you are saying](https://dic.nicovideo.jp/a/%E3%81%A1%E3%82%87%E3%81%A3%E3% 81% A8% E4% BD% 95% E8% A8% 80% E3% 81% A3% E3% 81% A6% E3% 82% 8B% E3% 81% 8B% E5% 88% 86% E3% 81% 8B% E3% 82% 89% E3% 81% AA% E3% 81% 84) That's right.
import numpy as np
from PIL import Image
import random
#Generator that returns cell state
def CA( rule_id, n_cell, init_cell_id = -1):
rule = list(map(int, f'{rule_id:08b}'))
#initial state
if init_cell_id < 0:
cell_c = [random.randint(0,1) for _ in range(n_cell)]
else:
#If an int value is given as the initial value, that bit pattern will be the initial state.
cell_c = list(map(int, f'{init_cell_id:b}'.zfill(n_cell)[-n_cell:]))
cell_c = np.array(cell_c)
yield cell_c
while True:
cell_n = [ rule[cell_c[i-1]*4 + cell_c[i]*2 + cell_c[(i+i) % n_cell]] for i in range(n_cell)] #Next state
cell_c = cell_n #Change of state
yield cell_c
random.seed(110)
#Data creation
W, H = 640, 480 #width,High
img = np.zeros((H, W))
ca = CA( 30, W) #Rule ID,Number of cells
for h, c in zip(range(H), ca):
img[h,:] = c
#Save as image
img = Image.fromarray(img*255).convert('RGB')
img.save('ret.png')