Vijay Kumar Attri
Vijay Kumar Attri

Reputation: 21

Pad an image with 10 Pixel with color of 0th pixel

My use case : I want to add a border of 10 pixel to the image using the ImageOps.expand method in the PIL. It asks for a fill value the default being black. I researched and found that in order to find the color value of a pixel, you need to do

pix = im.load() im = ImageOps.expand(im, border=10, fill=pix[0,0])

Issue: I am not able to pass this pix[x,y] value to the expand method. I went through the method definition and can't find the exact reason this is failing. This is the official documentation of the PIL that asks you to send a fill value.

I am able to get this kind of image. The only thing that is wrong is the annoying white border even though i am using the fill value as pix[0,0].

Upvotes: 2

Views: 735

Answers (1)

martineau
martineau

Reputation: 123463

The following works for me:

from PIL import Image, ImageOps

img = Image.open('skooter.png')

x, y = 0, 0
pix = img.load()
img2 = ImageOps.expand(img, border=10, fill=pix[x,y])
img2.show()

Result:

resulting image (img2)

Upvotes: 1

Related Questions