How to apply a unified absolute distortion map via Python/Wand

52 Views Asked by At

How can I apply ImageMagick’s unified distortion map via Python/Wand?

IM documentation: https://www.imagemagick.org/Usage/mapping/#distortion_maps

How it works in a shell script (IM 6.9):

convert "$content" \
    -resize 1700x1700\! \
    -matte "$map" \
    -compose Distort -composite "$distorted"

Or:

convert "$content" -resize 1700x1700\! -alpha set "$map" \
        -compose Distort -composite \
        "$distorted"

The resize brings the input image to the size of the map, as otherwise the result gets clipped.

I experimented with defining an image as content and using content.compose() or content.composite(), but any attempt at filling in arguments results in complaints.

1

There are 1 best solutions below

0
thorwil On

I figured it out after seeing an example with operator and noticing there’s alpha_channel. The solution is:

from wand.image import Image

with Image(filename='content.png') as c,\
     Image(filename='map.png') as m:
    c.resize(1700, 1700)
    c.alpha_channel = True
    c.composite(m, operator='distort')
    c.save(filename='distorted.png')

Thanks to everyone who took a look.