How to import index color mode images to a rgb color mode file?

I have a .ase file using the rgb color mode, and it works great to import via script dozens of rgb color mode .pngs. But if I try to convert the .pngs using color mode to rgb before importing to this file, it doesn’t work.

It looks like the image from the cel maintain the index colorMode.

I’m trying to use this line to convert to RGB colorMode.

local image = Image { fromFile= full_file_path, colorMode = ColorMode.RGB}

Any tip or any bulk converter that I could use to convert all pngs to RGB bero the import via this script?

Thanks.

Hi @phartz,

The script below is not very clean, but it’s what comes to mind. Test the image’s color mode. If the image is indexed, load the same path again, but as a palette. Then use the indexed image and palette to create an RGB copy.

local function indexedToRgb(sourceImage, sourcePalette)
    local sourceSpec = sourceImage.spec

    -- Assumes sRGB color space,
    -- sprite color space matches image color space, etc.
    -- targetSpec should default to RGB color mode,
    -- with 0 transparentColor.
    local targetSpec = ImageSpec {
        width = sourceSpec.width,
        height = sourceSpec.height
    }

    local targetImage = Image(targetSpec)
    local pixels = sourceImage:pixels()

    local sourceAlphaMask = sourceSpec.transparentColor
    for pixel in pixels do
        local index = pixel()
        local x = pixel.x
        local y = pixel.y

        if index == sourceAlphaMask then
            targetImage:drawPixel(x, y, 0)
        else
            local aseColor = sourcePalette:getColor(index)
            local i32Color = aseColor.rgbaPixel
            targetImage:drawPixel(x, y, i32Color)
        end
    end

    return targetImage
end

local full_file_path = "path\\to\\test.png"
local image = Image { fromFile = full_file_path }
if image.colorMode == ColorMode.INDEXED then
    local palette = Palette { fromFile = full_file_path }
    image = indexedToRgb(image, palette)
end

app.activeSprite:newCel(
    app.activeSprite:newLayer(),
    app.activeFrame,
    image,
    Point(0, 0))
app.refresh()

I didn’t explore the option, but you could also try loading the .png as a sprite, converting color mode with an app.command then closing the sprite when finished. Just so long as there is no confusion tracking which sprite is active.

Cheers,
Jeremy

2 Likes

Thanks, man. Gonna test it.

Thanks again, man. I tested it and the script works perfectly.

2 Likes