【问题标题】:Pad an image with 10 Pixel with color of 0th pixel用第 0 像素的颜色填充 10 像素的图像
【发布时间】:2019-03-05 15:15:43
【问题描述】:

我的用例: 我想使用 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].

【问题讨论】:

  • 您遇到的错误是什么?
  • edit您的问题并提供minimal reproducible example
  • @Mudits 它给出了一个白色边框。
  • 请提供您的实际输入图像,而不是结果图像旁边的图片。问题通常是图像本身的结果。
  • 我们可以使用其他库吗?

标签: python image image-processing python-imaging-library


【解决方案1】:

以下对我有用:

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

结果:

【讨论】:

  • 感谢您的及时回复。边框的颜色值与背景不同。边框需要是背景的延续。
  • pix[0, 0] is (0, 0, 0) 在我的测试图像中,所以显示的结果是正确的。如果我硬编码ImageOps.expand(img, border=10, fill=(255, 0, 0)),结果如预期的那样有一个红色边框。如果没有您的实际代码以及正在使用的实际图像,很难判断发生了什么。
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2013-05-07
  • 2021-10-07
  • 2015-01-19
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多