from PIL import Image src = r"C:\Users\Mahdi\.cursor\projects\e-projeha-crm\assets\mento-logo-mark.png" dst_png = r"e:\projeha\crm\apps\web\public\brand\mento-logo.png" img = Image.open(src).convert("RGBA") pixels = img.load() w, h = img.size for y in range(h): for x in range(w): r, g, b, a = pixels[x, y] # white canvas if r > 235 and g > 235 and b > 235: pixels[x, y] = (255, 255, 255, 0) continue # black / near-black canvas if r < 28 and g < 28 and b < 28: pixels[x, y] = (0, 0, 0, 0) continue # dark fringe around logo on black plate if r < 40 and g < 45 and b < 50 and (r + g + b) < 110: pixels[x, y] = (0, 0, 0, 0) bbox = img.getbbox() if not bbox: raise SystemExit("empty image after bg remove") img = img.crop(bbox) pad = 10 padded = Image.new("RGBA", (img.width + pad * 2, img.height + pad * 2), (0, 0, 0, 0)) padded.paste(img, (pad, pad), img) img = padded side = max(img.width, img.height) canvas = Image.new("RGBA", (side, side), (0, 0, 0, 0)) canvas.paste(img, ((side - img.width) // 2, (side - img.height) // 2), img) canvas.save(dst_png, "PNG") px = canvas.load() print("logo saved", dst_png, canvas.size, "corner", px[0, 0])