Coo*_*ies 16 python replace image colors python-imaging-library
我试图从我的图像中删除某种颜色,但它没有像我希望的那样好用.我尝试做同样的事情使用PIL使所有白色像素透明?然而,图像质量有点损失,因此它会在被移除的地方留下一些奇怪的彩色像素.如果所有三个值都低于100,我尝试做像更改像素这样的事情但由于图像质量差,周围的像素甚至都不是黑色.
有没有人知道用Python中的PIL更好的方法来替换它周围的颜色和任何东西?这可能是我能想到的完全移除物体的唯一确定的火力方式,但我想不出办法做到这一点.
图片为白色背景,文字为黑色.我们只想说我想从图像中完全删除文本而不留下任何文物.
非常感谢别人的帮助!谢谢
Nad*_*mli 24
最好的方法是使用Gimp中使用的"颜色到alpha"算法来替换颜色.它将完美适用于您的情况.我使用PIL为开源python照片处理器phatch重新实现了这个算法.你可以在这里找到完整的实现.这是一个纯粹的PIL实现,它没有其他依赖性.您可以复制功能代码并使用它.以下是使用Gimp的示例:
至 
您可以color_to_alpha使用黑色作为颜色在图像上应用该功能.然后将图像粘贴到不同的背景颜色上以进行替换.
顺便说一句,此实现使用PIL中的ImageMath模块.它比使用getdata访问像素更有效.
编辑:这是完整的代码:
from PIL import Image, ImageMath
def difference1(source, color):
"""When source is bigger than color"""
return (source - color) / (255.0 - color)
def difference2(source, color):
"""When color is bigger than source"""
return (color - source) / color
def color_to_alpha(image, color=None):
image = image.convert('RGBA')
width, height = image.size
color = map(float, color)
img_bands = [band.convert("F") for band in image.split()]
# Find the maximum difference rate between source and color. I had to use two
# difference functions because ImageMath.eval only evaluates the expression
# once.
alpha = ImageMath.eval(
"""float(
max(
max(
max(
difference1(red_band, cred_band),
difference1(green_band, cgreen_band)
),
difference1(blue_band, cblue_band)
),
max(
max(
difference2(red_band, cred_band),
difference2(green_band, cgreen_band)
),
difference2(blue_band, cblue_band)
)
)
)""",
difference1=difference1,
difference2=difference2,
red_band = img_bands[0],
green_band = img_bands[1],
blue_band = img_bands[2],
cred_band = color[0],
cgreen_band = color[1],
cblue_band = color[2]
)
# Calculate the new image colors after the removal of the selected color
new_bands = [
ImageMath.eval(
"convert((image - color) / alpha + color, 'L')",
image = img_bands[i],
color = color[i],
alpha = alpha
)
for i in xrange(3)
]
# Add the new alpha band
new_bands.append(ImageMath.eval(
"convert(alpha_band * alpha, 'L')",
alpha = alpha,
alpha_band = img_bands[3]
))
return Image.merge('RGBA', new_bands)
image = color_to_alpha(image, (0, 0, 0, 255))
background = Image.new('RGB', image.size, (255, 255, 255))
background.paste(image.convert('RGB'), mask=image)
Run Code Online (Sandbox Code Playgroud)
unu*_*tbu 14
使用numpy和PIL:
这会将图像加载到一个numpy形状的数组中(W,H,3),其中W宽度和H高度.数组的第三轴代表3个颜色通道R,G,B.
import Image
import numpy as np
orig_color = (255,255,255)
replacement_color = (0,0,0)
img = Image.open(filename).convert('RGB')
data = np.array(img)
data[(data == orig_color).all(axis = -1)] = replacement_color
img2 = Image.fromarray(data, mode='RGB')
img2.show()
Run Code Online (Sandbox Code Playgroud)
由于orig_color是长度为3的元组,并且data具有形状(W,H,3),因此NumPy
广播
orig_color到一个形状阵列(W,H,3)以执行比较data ==
orig_color.结果是一个布尔形状的数组(W,H,3).
(data == orig_color).all(axis = -1)是形状的布尔阵列(W,H)无论在RGB颜色哪个是真的data是original_color.
#!/usr/bin/python
from PIL import Image
import sys
img = Image.open(sys.argv[1])
img = img.convert("RGBA")
pixdata = img.load()
# Clean the background noise, if color != white, then set to black.
# change with your color
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y] == (255, 255, 255, 255):
pixdata[x, y] = (0, 0, 0, 255)
Run Code Online (Sandbox Code Playgroud)
您需要将图像表示为二维数组.这意味着要么制作像素列表列表,要么将1维数组视为具有一些聪明数学的2d数组.然后,对于每个目标像素,您需要找到所有周围的像素.你可以用python生成器这样做:
def targets(x,y):
yield (x,y) # Center
yield (x+1,y) # Left
yield (x-1,y) # Right
yield (x,y+1) # Above
yield (x,y-1) # Below
yield (x+1,y+1) # Above and to the right
yield (x+1,y-1) # Below and to the right
yield (x-1,y+1) # Above and to the left
yield (x-1,y-1) # Below and to the left
Run Code Online (Sandbox Code Playgroud)
所以,你会像这样使用它:
for x in range(width):
for y in range(height):
px = pixels[x][y]
if px[0] == 255 and px[1] == 255 and px[2] == 255:
for i,j in targets(x,y):
newpixels[i][j] = replacementColor
Run Code Online (Sandbox Code Playgroud)
如果像素不容易识别,例如您所说的(r < 100 和 g < 100 且 b < 100)也与黑色区域不正确匹配,则意味着您有很多噪声。
最好的方法是识别一个区域并用您想要的颜色填充它,您可以手动识别该区域,也可以通过边缘检测来识别,例如http://bitecode.co.uk/2008/07/edge-detection-in-python /
或者更复杂的方法是使用像 opencv ( http://opencv.willowgarage.com/wiki/ ) 这样的库来识别对象。