How can a cube/ a group of cubes be painted? Setup : Blender 2.82

I want to achieve something similar to what was represented below but through a  python script.

If each cube here represents a pixel, what is the fastest way to change its color so it looks exactly like the 2d image?


I wonder if it is possible to put there an actual RGB photo and not just a number? 
I am also curios if i can map on a face of an cube a full image like a feature map from a tensorflow model ? 


Here is the code that i have atm for creating cubes at different location in different collections

def createCubes(x,y,z, name):
  collection = bpy.data.collections.new(name= name) 
  bpy.context.scene.collection.children.link(collection) 
  k = 0
  l = 0
  for i in range(0,int(z)):
      for j in range(0, int(x)):
        bpy.ops.mesh.primitive_cube_add(location=(k, y,  l ))
        bpy.context.active_object.name = 'cube'
        cube = bpy.context.object
        for ob in cube.users_collection[:]: 
            ob.objects.unlink(cube)
        collection.objects.link(cube) 
        k= k+3
      l = l + 3
      k = 0
you have to use opencv a python library for image processing so that you can map a full image.