import sharp from 'sharp' import { writeFileSync } from 'fs' import { fileURLToPath } from 'url' import { dirname, join } from 'path' const __dirname = dirname(fileURLToPath(import.meta.url)) const input = join(__dirname, '../src/assets/profile/avatar.png') const { data, info } = await sharp(input) .ensureAlpha() .raw() .toBuffer({ resolveWithObject: true }) for (let i = 0; i < data.length; i += 4) { const r = data[i] const g = data[i + 1] const b = data[i + 2] const sum = r + g + b const isBlack = sum < 55 const isNavy = b > r + 8 && b > g + 2 && sum < 140 const isDarkBlueBg = b > 15 && r < 35 && g < 45 && b < 90 && sum < 100 if (isBlack || isNavy || isDarkBlueBg) { data[i + 3] = 0 } } const output = await sharp(data, { raw: { width: info.width, height: info.height, channels: 4 }, }) .png() .toBuffer() writeFileSync(input, output) console.log('Done:', info.width, 'x', info.height)