local spr = app.activeSprite if not spr then app.alert("No active sprite") return end local path = app.fs.filePath(spr.filename) local name = app.fs.fileTitle(spr.filename) if path == "" then path = app.fs.userDocsPath end local out_path = app.fs.joinPath(path, name .. ".ppm") local img = Image(spr.width, spr.height, ColorMode.RGB) -- Render visible layers / current frame into one image img:drawSprite(spr, app.activeFrame.frameNumber) local file = io.open(out_path, "wb") if not file then app.alert("Could not write file:\n" .. out_path) return end -- PPM binary format, P6 file:write("P6\n") file:write(tostring(spr.width) .. " " .. tostring(spr.height) .. "\n") file:write("255\n") for y = 0, spr.height - 1 do for x = 0, spr.width - 1 do local pixel = img:getPixel(x, y) local c = Color(pixel) file:write(string.char(c.red)) file:write(string.char(c.green)) file:write(string.char(c.blue)) end end file:close() app.alert("Exported PPM:\n" .. out_path)