Python: How to Save UV Layout With Specific Background Color? select_obj(some_obj)
bpy.ops.object.mode_set(mode=&quot;EDIT&quot;)
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
# how to set background color of this image?
bpy.ops.uv.export_layout(filepath='uv_layout.png', size=(1024, 1024), opacity=1)
bpy.ops.object.mode_set(mode=&quot;OBJECT&quot;)
bpy.ops.object.select_all(action='TOGGLE')

The image looks like this (it has a transparent background):

However, I want to achieve a colored background like this:

I've tried to import PIL and do the image manipulation with this module, but it seems as if it isn't included in Blender.
It is not that hard to overwrite the function provide by the module itself (it is still in Python scope):
import io_mesh_uv_layout
from io_mesh_uv_layout.export_uv_png import *

def new_export(filepath, face_data, colors, width, height, opacity):
    offscreen = gpu.types.GPUOffScreen(width, height)
    offscreen.bind()

    try:
        bgl.glClearColor(0.0, 1.0, 0.0, 0.9)  # Here, a green, 0.9 alpha background
        bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
        draw_image(face_data, opacity)

        pixel_data = get_pixel_data_from_current_back_buffer(width, height)
        save_pixels(filepath, pixel_data, width, height)
    finally:
        offscreen.unbind()
        offscreen.free()

io_mesh_uv_layout.export_uv_png.export = new_export  # Directly overwrite it



In your addon, you can provide a color picker(from a color float property) for user. And after updating the value, patch the original export function on the fly. There shouldn't be any tricky part and no performance issue at all.

A more complex method is to modify the uv layout export itself:

Which needs to modify the operator itself, kind of tricky but more flexible to use:
 (Comment included)