I want to produce a Python algorithm which takes in a 'mask' RGB image comprised exclusively of black and white pixels. Basically, each mask is a black image with one or more white shapes on it (see below).
I want to transform this image by enlarging the white areas by a factor x:
So far I have only got it to work by drawing rectangles around the shapes using PIL:
def add_padding_to_mask(mask, padding):
# Create a copy of the original mask
padded_mask = mask.copy()
draw = ImageDraw.Draw(padded_mask)
# Iterate over the pixels in the original mask
for x in range(mask.width):
for y in range(mask.height):
# If the pixel is white, draw a white rectangle with the desired padding around it
if mask.getpixel((x, y)) == (255, 255, 255):
draw.rectangle((x-padding, y-padding, x+padding, y+padding), fill=(255, 255, 255))
return padded_mask
This is suboptimal since I want to retain the original white shapes (only make them larger). I can't figure out an efficient way to approach this problem. Any help greatly appreciated.