python - 使用 Imagemagick 查找相似颜色的区域

标签 python image colors imagemagick pixel

是否有任何方法可以使用 ImageMagick(或类似的东西,任何可行的东西!)来查找图像中相邻像素颜色相似的区域?

提前致谢,

最佳答案

Photoshop、Gimp 和许多其他图像处理器将为您完成此操作。通过编程,以下是一些 Python 代码可以完成此任务:

from PIL import Image, ImageDraw

def inImage(im, px):
    x,y = px
    return x < im.size[0] and y < im.size[1] and x > 0 and y > 0

def meetsThreshold(im, px, st, threshold):
    color = im.getpixel(px)
    similar = im.getpixel(st)
    for cPortion, sPortion in zip(color,similar):
        if abs(cPortion - sPortion) > threshold:
            return False
    return True

def floodFill(im, fillaroundme, fillWith, meetsThresholdFunction):
    imflooded = im.copy()
    imflooded.putpixel(fillaroundme, fillwith)
    processed = []
    toProcess = [fillaroundme]
    while len(toProcess) > 0:
        edge = toProcess.pop()
        processed.append(edge)
        x, y = edge
        for checkMe in ((x+1, y), (x-1, y), (x, y+1), (x, y-1)):
            if inImage(im, checkMe) and meetsThresholdFunction(im, edge, checkMe):
                imflooded.putpixel(checkMe, fillWith)            
                if checkMe not in toProcess and checkMe not in processed:
                    toProcess.append(checkMe)
        processed.append(edge)
    return imflooded


im = Image.open(r"tardis.jpg")
filled = floodFill(im, (120, 220), (255, 0, 0), lambda im, px, st: meetsThreshold(im, px, st, 10))

filled.show()

我从 here 得到了 tardis.jpg

关于python - 使用 Imagemagick 查找相似颜色的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7922281/

相关文章:

c# - 如何拉伸(stretch)图像以填充此 WPF/XAML 应用程序?

DELPHI - 如何在 VCL 表单应用程序中更改 TButton 背景颜色?

python - 整数和整数的正则表达式?

异常处理

python - 猜猜Python中的数字。关闭游戏的选项

python - matplotlib 中的自定义绘图线型

image - 如何从 Apple 获取旧版本的正版 iOS 图像?

python - 改变图像的深度?

r - 具有 Gamma 参数的渐变色标?

c++ - OpenGL 'reshape()' 功能视口(viewport)边框背景