imagegraphicsimagemagickinterlacing

How can I interlace two images using ImageMagick?


I have 2 images of the same size, called image1.png and image2.png. In ImageMagick, is there any way to merge the two images by taking the odd lines from image1.png and merge with the even line from image2.png ?


Solution

  • Sure, make alternate rows transparent:

    # Make red test image
    convert -size 300x200 xc:red red.png
    

    enter image description here

    # Make blue test image
    convert -size 300x200 xc:blue blue.png
    

    enter image description here

    # Merge with alternate lines made transparent
    convert red.png \( blue.png -alpha on -channel A -fx 'j%2' \) -composite result.png
    

    enter image description here

    Or, an alternative way of thinking about it is to load both images and then choose pixels from either the first (u) or the second (v) depending on the row:

    convert red.png blue.png -fx 'j%2 ? u : v' result.png
    

    enter image description here

    On Windows, these two come out as:

    REM Do Windows style commands
    convert red.png ^( blue.png -alpha on -channel A -fx "j%2" ^) -composite result.png
    

    and

    REM Windows style
    convert red.png blue.png -fx "j%2 ? u:v" result.png
    

    If you want vertical interlace instead of horizontal interlace, just change the j to i in all above examples.