使用python棒替换颜色,但坐标未知

2024-04-25 16:36:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道Replace a color using Wand和{a2},但它们都使用类似于

    draw.color(192,84,'replace')

其中你需要传递一个相关颜色像素的位置。如果你知道要替换的颜色,但不知道它的位置,怎么办?我想替换图像中像素的颜色,而不传递对该颜色像素位置的引用。你真的要扫描整个图像寻找你已经知道的东西吗?在

imagemagick的等价物是

^{pr2}$

Tags: 图像a2颜色像素wandreplaceimagemagickcolor
2条回答

由于wand0.5.4的方法opaque_paint是{a1},所以不再需要提出的聪明的hack@emcconville了。您只需:

from wand.image import Image
with Image(filename='rose:') as im:
  im.opaque_paint(target='#E93A43', fill='Orange', fuzz=0.10)
  im.save(filename='output.png')

如果您想匹配-opaque功能,那么您需要实现MagickOpaquePaintImageC方法。在

import ctypes
from wand.api import library
from wand.image import Image
from wand.color import Color
from wand.compat import nested

# Map C-API to Python
library.MagickOpaquePaintImage.argtypes = (ctypes.c_void_p,  # Wand
                                           ctypes.c_void_p,  # target
                                           ctypes.c_void_p,  # fill
                                           ctypes.c_double,  # fuzz
                                           ctypes.c_bool)    # invert

with Image(filename='rose:') as img:
    with nested(Color('#E93A43'), Color('ORANGE')) as (target, fill):
        library.MagickOpaquePaintImage(img.wand,
                                       target.resource,
                                       fill.resource,
                                       img.quantum_range * 0.10, # -fuzz 10%
                                       False)
    img.save(filename='output.png')

output.png

相关问题 更多 >