The colors that I get in Python directly from the nodes are darker when written to an image file
Credit to: @MarkyMarg2 https://twitter.com/MarkyMarg2/status/1290834859875692547 for solving this.
The colors need to be gamma corrected.
Each channel of RGB needs to be run through this formula:
if c &lt; 0.0031308:
    srgb = 0.0 if c &lt; 0.0 else c * 12.92
else:
    srgb = 1.055 * math.pow(c, 1.0 / 2.4) - 0.055

Here's a working example:
import bpy

size = 16, 16
image = bpy.data.images.new(&quot;palette&quot;, width=size[0], height=size[1])

carray = []
cindex = 0

def to_srgb(c):
    if c &lt; 0.0031308:
        srgb = 0.0 if c &lt; 0.0 else c * 12.92
    else:
        srgb = 1.055 * pow(c, 1.0 / 2.4) - 0.055

    return srgb

obj = bpy.context.active_object
for m in obj.material_slots:
    if m.name:
        #print(str(m.material.diffuse_color[:]))
        #carray.append(m.material.diffuse_color)
        if m.material.use_nodes:
            for n in m.material.node_tree.nodes:
                if n.type=='EMISSION':
                    color = n.inputs[0].default_value
                    carray.append(color)
        #else:
            #carray.append(m.material.diffuse_color)
            
                    
print(carray)

pixels = [None] * size[0] * size[1]
for y in range(size[1]):
    for x in range(size[0]):
        if cindex &lt; len(carray):
            c = carray[cindex]
            color_fixed = [1.0,1.0,1.0,1.0]
            color_fixed[0] = to_srgb(c[0])
            color_fixed[1] = to_srgb(c[1])
            color_fixed[2] = to_srgb(c[2])
            color = color_fixed
        else :
            color = [1.0,1.0,1.0,1.0]

        print(cindex)
        print(color)
        pixels[(abs(y-15) * size[0]) + x] = color#[r, g, b, a]
        cindex=cindex+1

# flatten list
pixels = [chan for px in pixels for chan in px]

# assign pixels
image.pixels = pixels

# write image
image.filepath_raw = &quot;/temp/palette.png&quot;
image.file_format = 'PNG'
image.save()