Search code examples
pythonimagemagickimagemagick-convertwand

Defining morphology:compose for merging results of a multi-morphology kernel using Wand


I'm trying to remove lines from an image using the method outlined in this answer: https://stackoverflow.com/a/46533742, but I want to do so using the python Wand library. So far, I have

with Image(filename=file_path) as img:
        print(img.size)
        display(img)
        with img.clone() as img_copy:

            # Trying to replicate the following command: 
            # convert <image-file-path> -type Grayscale -negate -define morphology:compose=darken -morphology Thinning 'Rectangle:1x80+0+0<' -negate out.png

            img_copy.type = 'grayscale'
            img_copy.negate()
            img_copy.morphology(method="thinning", kernel="Rectangle:1x80+0+0<")
            img_copy.negate()
            display(img_copy)

I have been trying to figure out what the equivalent command for -define morphology:compose=darken is but have had no luck. According to https://imagemagick.org/script/defines.php#morphology this line is used to "specify how to merge results generated by multiple-morphology kernel," hence the title of this question. Is this possible using wand?


Solution

  • I believe you want to use the Image.artifacts dict.

    with Image(filename=file_path) as img:
        with img.clone() as img_copy:
            # Trying to replicate the following command: 
            # -type Grayscale
            img_copy.type = 'grayscale'
            # -negate 
            img_copy.negate()
            # -define morphology:compose=darken
            img_copy.artifacts['morphology:compose'] = 'darken'
            # -morphology Thinning 'Rectangle:1x80+0+0<'
            img_copy.morphology(method="thinning", kernel="Rectangle:1x80+0+0<")
            # -negate
            img_copy.negate()
            display(img_copy)