Skip to main content
 首页 » 编程设计

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

2024年09月07日8Renyi-Fan

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

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

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

imagemagick 等价物是

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

请您参考如下方法:

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