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

时间:2018-02-21 21:10:20

标签: python replace colors imagemagick wand

我知道Replace a color using WandChange color of specific pixels [Wand],但这两个都使用像

这样的行
    draw.color(192,84,'replace')

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

imagemagick等效于

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

2 个答案:

答案 0 :(得分:1)

如果您想匹配-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')

output.png

答案 1 :(得分:1)

由于魔杖是0.5.4,因此方法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') available,因此不再需要提出聪明的@emcconville技巧。您可以这样做:

    // Create a Venmo component.
      var venmoButton = document.getElementById('venmo-button');
      braintree.venmo.create({
        client: clientInstance
      },function (venmoErr, venmoInstance) {
        if (venmoErr) {
          console.error('Error creating Venmo:', venmoErr);
          return;
        }

        Verify browser support before proceeding.
        if (!venmoInstance.isBrowserSupported()) {
          console.log('Browser does not support Venmo');
          return;
        }
        //moved inside Meteor.call
        function displayVenmoButton(venmoInstance) {
          // Assumes that venmoButton is initially display: none.
          venmoButton.style.display = 'block';

          venmoButton.addEventListener('click', function () {
            venmoButton.disabled = true;

            venmoInstance.tokenize(function (tokenizeErr, payload) {
              venmoButton.removeAttribute('disabled');

            });
          });
        }