How to remove pixels with alpha lower than a threshold?

So far, all I’ve been able to do is apply changes (like adjusting alpha) to all pixels using filters, but I couldn’t find a way to select or target only those pixels with low alpha.

The alpha channel can be isolated with the replace color command as well, if that’s any use… But I’d probably just write a Lua script.

Click on the arrow to the left to see example script
local threshold = 128

local sprite = app.sprite
if not sprite then return end
if sprite.colorMode ~= ColorMode.RGB then return end

local layer = app.layer
if not layer then return end
if layer.isReference then return end
if layer.isTilemap then return end

local cel = app.cel
if not cel then return end

local srcImage = cel.image
local wSrc = srcImage.width
local hSrc = srcImage.height
local trgImage = Image(srcImage.spec)

for y = 0, hSrc - 1, 1 do
    for x = 0, wSrc - 1, 1 do
        local srcPixel = srcImage:getPixel(x, y)
        local a8 = app.pixelColor.rgbaA(srcPixel)
        local r8, g8, b8 = 0, 0, 0
        if a8 >= threshold then
            r8 = app.pixelColor.rgbaR(srcPixel)
            g8 = app.pixelColor.rgbaG(srcPixel)
            b8 = app.pixelColor.rgbaB(srcPixel)
            a8 = 255
        else
            a8 = 0
        end
        local trgPixel = app.pixelColor.rgba(r8, g8, b8, a8)
        trgImage:drawPixel(x, y, trgPixel)
    end
end

cel.image = trgImage
app.refresh()

The example above sets the active image’s alpha to 255 if it is greater than or equal to a threshold, 128, or to 0 if it is less than the threshold. Selecting pixels, removing translucency through thresholding and removing pixels from an image based on zero alpha pixels, however, are three separate things. So I’m not sure what you’re looking for.

For trimming alpha, look through the docs for https://aseprite.org/api/command/CanvasSize or https://aseprite.org/api/image#imageshrinkbounds in conjunction with https://aseprite.org/api/image#imagedrawimage . For selections, see https://aseprite.org/api/selection.

I put a more involved remove alpha script in the beta branch of this code repo if you’re interested: AsepriteAddons/dialogs/color/removeAlpha.lua at Beta · behreajj/AsepriteAddons · GitHub . The downsides are that the fancier script depends on many others to work, and, the beta Lua script should be run with the beta version of Aseprite.

There is also a select by criteria script which includes alpha as a criterion.

That was exactly what I was looking for, thank you!