Remove border from Logo using Python PIL


Hi I want to remove the white border from this Logo using python’s pillow library. The only way I have thought of is to extract all white from the image but then this remove’s the white eye of the horse as well and that is something I would like to keep.

What I have.

What I want.

This isn’t a daunting task but there are a lot of logo’s that I need to do this for so I would like an automated fashion of doing so. Here is some code to extract the image from the source. Thanks for any help anyone can provide.

from PIL import Image
import pandas as pd
import requests

filename = ""

image =, stream=True).raw)


I prefer using OpenCV but we can do it with PIL.

  • Replace white with transparency using the example from here
  • Get the alpha channel.
    Use ImageDraw.floodfill for filling the surrounding zero alpha with 255 color.
    (Only the eye stays black).
  • Invert alpha – make the eye white instead of black.
  • Paste the white eye on the image with the "transparent white".

Code sample (reading local image):

from PIL import Image, ImageDraw, ImageOps
import numpy as np

def white_to_transparency(img):
    x = np.asarray(img.convert('RGBA')).copy()
    x[:, :, 3] &= (255 * (x[:, :, :3] != 255).any(axis=2)).astype(np.uint8)  # Small modification: &= is used instead of = (in the original code).
    return Image.fromarray(x)

filename = "68.png"

image =

im_white_transparent = white_to_transparency(image)

# Extract alpha channel as new Image
alpha = im_white_transparent.getchannel('A')

# Fill alpha channel with 255, only the inner part stays 0
ImageDraw.floodfill(alpha, xy=(0, 0), value=255, thresh=200)

mask = ImageOps.invert(alpha)  # Invert alpha - make the eye white instead of black
im_white_transparent.paste(image, (0, 0), mask)  # Paste the white eye on the image with "transparent white".

# Show images for testing


(change to dark mode for seeing the transparent background):
enter image description here

Same result with transparency as chessboard pattern:
enter image description here

enter image description here

Answered By – Rotem

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.