简体   繁体   English

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

[英]Replace a color using python Wand but coordinates unknown

I am aware of Replace a color using Wand and Change color of specific pixels [Wand] but both of these use a line like我知道使用 Wand 替换颜色更改特定像素 [Wand] 的颜色,但这两者都使用类似的线

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

In which you need to pass the location of a pixel of the relevant color.您需要在其中传递相关颜色的像素的位置。 What if you know the color you want to replace but not its location?如果您知道要替换的颜色但不知道其位置怎么办? I want to replace the color of pixels in an image without passing a reference to the location of a pixel of that color.我想替换图像中像素的颜色,而不传递对该颜色像素位置的引用。 Do you really have to scan the entire image looking for something you already know is there?你真的需要扫描整个图像来寻找你已经知道的东西吗?

The imagemagick equivalent would be imagemagick 等价物是

convert balloon.gif -fill white -opaque blue balloon_white.gif

If you want to match -opaque functionality, then you'll need to implement the MagickOpaquePaintImage C method.如果要匹配-opaque功能,则需要实现MagickOpaquePaintImage C 方法。

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

输出.png

Since wand 0.5.4 the method opaque_paint is available so the clever hack @emcconville proposed is not required anymore.由于棒0.5.4方法opaque_paint可以这样提出的巧妙破解@emcconville现在不再需要了。 You can do just:你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM