繁体   English   中英

用魔杖以预定义图案填充图像

[英]Fill an image with pre-defined pattern with Wand

我有一个图像(如: mask )和两个整数,它们代表最终的图像宽度和高度。 根据Wand的文档打开空白图片

with Image(width=200, height=100) as img:
    img.save(filename='200x100-transparent.png')

这将导致具有透明背景的空白图像。 现在,问题是:如何创建相同的空图像,但以遮罩图像作为背景图案?

composite CLI命令本身具有以下运算符:

-tile repeat composite operation across and down image

但是如何用魔杖实现同样的效果呢?

好了,在查看了ImageMagick的Composite 源代码本身之后,很明显,Wand驱动的解决方案应如下所示:

with Image(width=x, height=y) as img:
    for x in xrange(0, img.width, crop_mask_path.width):
        for y in xrange(0, img.height, crop_mask_path.height):
            img.composite_channel('default_channels', crop_mask_path, 'over', x, y)
    img.save(filename='patterned_image.png')

在我看来,构建标题迭代器是最好的解决方案 但是,另一种令人讨厌的方法是调用tile:协议,并允许内部ImageMagick方法处理合成。 您将失去DIY继承的控件,但是在优化的IM系统上可以获得一些性能。

from wand.image import Image
from wand.api import library

with Image() as img:
   # Same as `-size 400x400' needed by tile: protocol.
   library.MagickSetOption(img.wand, 'size', '400x400')
   # Prefix filename with `tile:' protocol.
   img.read(filename='tile:rose.png')
   img.save(filename='tile_rose.png')

带有魔杖的预定义图案

暂无
暂无

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

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