‘mat’ is a 1000000*16 Boole Matrix, and I want to convert the matrix to a photo.(1 is white, 0 is black)
There is something wrong when I utilise ‘@ti.kernel’, however, ‘@ti.pyfunc’ is fine. Could u plz tell me the reason? And how to fix it if I want to use ‘@ti.kernel’? Thank you in advance.

import taichi as ti
import tomatrix
ti.init(arch=ti.gpu)
mat=tomatrix.mat
shape = (16, len(mat))
type = ti.u8
pixels = ti.var(dt=type, shape=shape)
#print(mat[0][2])
@ti.kernel
def draw():
for i in range(len(mat)):
for j in range (16):
if mat[i,j] == 1:
pixels[j,i] = 255 # integars between [0, 255] for ti.u8
else:
pixels[j,i] = 0
draw()
ti.imwrite(pixels, f"export.png")

Python list cannot be passed into Taichi kernels directly for now. You’ll need to convert them into numpy.array first:

import taichi as ti # taichi is a professnional package about computer graphics
import numpy as np # numpy is a professional package about arrays
import tomatrix # your tomatrix.py
ti.init(arch=ti.gpu)
mat=np.array(tomatrix.mat) # assume your `tomatrix.mat` is a Python list
shape = mat.shape # numpy will automatically compute the shape for you :)
type = ti.u8
pixels = ti.var(dt=type, shape=shape)
@ti.kernel
def draw(m: ti.ext_arr()): # ti.ext_arr() tells Taichi this is an external array (np.array or torch.Tensor)
for i, j in pixels: # same as two for-loops over pixels.shape, only works in @ti.kernel
if mat[i,j] == 1:
pixels[i, j] = 255 # IIRC Taichi images are row-major (x, y)
else:
pixels[i, j] = 0
draw()
ti.imwrite(pixels, f"export.png")