Mentions légales du service

Skip to content
Snippets Groups Projects
Commit e5b2eb61 authored by Nazim@misirlou's avatar Nazim@misirlou
Browse files

nzm

parent f1fd2575
No related branches found
No related tags found
No related merge requests found
import sys
# Importing the Image module from the PIL library
from PIL import Image
# Importing the NumPy library and aliasing it as 'np'
import numpy as np
####
VERBOSE=False
K=10 # scaling factor
COLDEF=[10,20,30] #default
COLP=[100,100,155] #.
COL0=[255,255,255] #0
COL1=[0,0,255] #1
# correspondance color and characters
TABCOL=[COLP,COL0,COL1]
TABPOS=".01"
def matrix2image(nparray,X,Y,filenameout):
# Creating an image object 'img' from the NumPy array 'data'NP
# specifying the color mode as 'RGB'
img = Image.fromarray(nparray, 'RGB')
img = img.resize((K*X,K*Y),resample=Image.BOX) #Image.LANCZOS
img.save(filenameout)
print("---wrote:%s"%filenameout)
def readfile(filename):
f = open(filename, "r")
dat=[]
maxlen=0 # maxlen of a line
for x in f:
x= x.rstrip() # removes trailing whitespaces and newlines
dat.append(x)
if (len(x)>maxlen):
maxlen=len(x)
if VERBOSE:
for line in dat:
print(line)
return (dat,maxlen,len(dat))
def color(charin):
""" char to color"""
pos=TABPOS.index(charin)
return TABCOL[pos]
def array2NParray(arrayIn,X,Y):
nparray = np.zeros((Y, X, 3), dtype=np.uint8)
for j in range(Y):
line=arrayIn[j]
# parsing one line
# print("debug line:%s",line)
for i in range(X):
if i<len(line):
char=line[i]
nparray[j][i]= color(char)
else:
nparray[j][i]=COLDEF
#print("i,j,v %d,%d,%s"%(i,j,nparray[j][i]))
return nparray
def transformToImage(filename):
content,X,Y=readfile(filename)
print("XY: (%d,%d)"%(X,Y))
nparray=array2NParray(content,X,Y)
filenameout=filename+".png"
matrix2image(nparray,X,Y,filenameout)
# main
if (len(sys.argv)<2):
print("give filenames txt to convert")
sys.exit()
for i in range(len(sys.argv)-1):
filename=sys.argv[i+1]
transformToImage(filename)
N=4
M=2**N
A= [[0] * M for i in range(M) ] # waw! creatio of a two-dminesional array
# local transition rule in thirds
PTRANS=[0, 2, 2, 0]
# from decimal to binarray
def arrayconfig(iconfig):
array=[]
for i in range(N):
bit=iconfig%2
array.append(bit)
iconfig//=2
return array
# one active transiton in place pos
def transAc(x, pos):
y= x.copy()
y[pos]= 1 - y[pos]
return y
# assume square matrix
def prettyprint(matrix):
for i in range(len(matrix)):
print(matrix[i])
################################
for iconfig in range(M):
x= arrayconfig(iconfig)
for pos in range(N):
T1= transAc(x, pos) if x[pos]==0 else x
T0= transAc(x, pos) if x[pos]==1 else x
index1= iconfig + 2 **pos if x[pos]==0 else iconfig
index0= iconfig - 2 **pos if x[pos]==1 else iconfig
s=x[pos-1]+x[pos]+x[(pos+1)%N]
part1=" ic:%d(%s) pos:%d S:%d"%(iconfig, x, pos, s)
parta= "i1:%d(%s) %d"%(index1, str(T1), PTRANS[s])
partb= "i0:%d(%s) %d"%(index0, str(T0), 3- PTRANS[s])
print(part1, parta, partb)
A[iconfig][index1]+= PTRANS[s]
A[iconfig][index0]+= 3 - PTRANS[s]
print("--")
prettyprint(A)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment