简体   繁体   English

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

[英]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.我的用例:我想使用 PIL 中的 ImageOps.expand 方法向图像添加 10 像素的边框。 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.问题:我无法将此pix[x,y]值传递给 expand 方法。 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. 是 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].唯一错误的是烦人的白色边框,即使我使用填充值作为 pix[0,0]。

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:结果:

结果图像 (img2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM