Skip to main content
 首页 » 编程设计

python之用第 0 个像素的颜色填充 10 个像素的图像

2024年11月24日10bluestorm

我的用例: 我想使用 PIL 中的 ImageOps.expand 方法为图像添加 10 像素的边框。它要求填充值默认为黑色。 我研究发现,为了找到一个像素的颜色值, 你需要做的

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

问题: 我无法将此 pix[x,y] 值传递给 expand 方法。我浏览了 method definition并且找不到失败的确切原因。 This是PIL的官方文档,要求你发送一个填充值。

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].

请您参考如下方法:

以下对我有用:

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() 

结果: