0

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 ?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
CSDD
  • 339
  • 2
  • 14

1 Answers1

0

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I'm using command prompt on Windows for ImageMagick so I guess there are some compatibility problems. The first method doesn't work. Using backslash in cmd results in error. I tried to use your alternative method, albeit replace red and blue with my images' names, and the CPU shoots up too 100% for a few seconds, which may seem normal as for rendering image but then no output file. – CSDD Apr 01 '16 at 13:07
  • On Windows, you need to replace single quotes with double quotes. You may also need a caret (`^`) before brackets and percent signs, or to double percent signs. – Mark Setchell Apr 01 '16 at 13:11
  • I have done some Windowsy versions at the end for you. – Mark Setchell Apr 01 '16 at 13:23
  • Works like a charm Thank you – CSDD Apr 01 '16 at 14:16
  • Pleasure! Good luck with your project. – Mark Setchell Apr 01 '16 at 14:24