Manipulating images

Hi, I used something like:

import matplotlib.pyplot as plt
img = plt.imread("my_image.png")
(Nx, Ny) = img.shape

mesh = UnitSquareMesh(Nx, Ny, "crossed")

class FE_image(UserExpression):
    def eval_cell(self, value, x, ufc_cell):
        p = Cell(mesh, ufc_cell.index).midpoint()
        i, j = int(p[0]*(Nx-1)), int(p[1]*(Ny-1))
        value[:] = img[-(j+1), i]

    def value_shape(self):
        return ()

y = FE_image()

y is then an Expression that you can interpolate or project as you want.

1 Like