【问题标题】:PIL replacing colour without affecting edgesPIL 替换颜色而不影响边缘
【发布时间】:2014-10-07 15:39:45
【问题描述】:

我有一个用黑线绘制的相对复杂的图像,我想将背景颜色从白色更改为随机颜色。

使用 PIL 我将所有非黑色像素替换为颜色:

  pixels = output.getdata()
  newPixels = []

  for pixel in pixels:
    if pixel[0] != 0 and pixel[1] != 0 and pixel[2] != 0:
      newPixels.append(color)
    else:
      newPixels.append(pixel)

  output.putdata(newPixels)

当我替换边缘平滑而不是完全黑色像素时,这会使线条全部呈锯齿状。有什么解决办法吗?

【问题讨论】:

  • 你能给我们看看图片吗?如果是纯灰度,我有个主意。
  • 边缘检测图像,然后避免改变这些区域的像素。

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


【解决方案1】:

看来您想保留纯黑色并替换纯白色。其他一切都需要混合。

这是最简单的混合方法:

newPixels.append([pixel[i] * color[i] // 255 for i in (0,1,2)])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2020-12-12
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多