Search code examples
pythonimagemagickcompositewand

Fill an image with pre-defined pattern with Wand


I have an image (like that: mask) and two integers, which represents a final image width & height. According to Wand's documentation Open empty image:

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

It will result in an empty image with transparent background. Now, the question is: How to create a same empty image, but with mask image as background pattern?

The composite CLI command itself has a following operator:

-tile repeat composite operation across and down image

But how to achieve the same with Wand?


Solution

  • Well, after looking on ImageMagick's Composite source code itself, it became clear, that the Wand-driven solution should look like:

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