I call this operation 'smushing' which is what the OLD ascii font program 'figlet' calls it.
The ideal solution is quite straight forward, though not 'simple'.
Assuming the images are the same width. -- THEY ARE NOT!
I expanded two.png to 255 pixels wide centred for this example.
Get the alpha channel of the two images and threshold them using a threshold of '0' so only perfect transparency is used.
Code: Select all
convert one.png -alpha extract -threshold 0 one_alpha.gif
convert two.png -alpha extract -threshold 0 two_alpha.gif
now use
morphology thicken, to expand the top edge of the alpha shape of one image all the way to the edge. Repeat but for the bottom edge of the other image.
Code: Select all
convert one_alpha.gif -morphology thicken:-1 1x2+0+0:0,1 one_thicken.gif
convert two_alpha.gif -morphology thicken:-1 1x2+0+1:1,0 two_thicken.gif
Now add up the columns by using a
DIY distance function and extract result.
Code: Select all
convert one_thicken.gif -morphology distance:-1 1x2+0+1:1,0 \
-gravity north -extent x1 +depth -compress none pgm:
convert two_thicken.gif -morphology distance:-1 1x2+0+0:0,1 \
-gravity south -extent x1 +depth -compress none pgm:
Add the column numbers of the two images. (you could skip the previous steps, if they are the same width)
Code: Select all
convert \( one_thicken.gif -morphology distance:-1 1x2+0+1:1,0 \
-gravity north -extent x1 \) \
\( two_thicken.gif -morphology distance:-1 1x2+0+0:0,1 \
-gravity south -extent x1 \) \
-compose plus -composite +depth -compress none pgm:
P2
255 1
65535
0 0 60 63 64 65 66 67 67 68 68 68 68 68 68 68 68 68 68 68 68 123 122 122 121
121 120 118 117 114 55 55 55 55 55 55 55 55 55 55 55 122 122 122 122 122 121
54 53 52 51 50 48 0 0 0 67 67 67 67 67 67 47 40 38 37 36 53 52 52 92 92 92 92
92 93 95 100 124 124 124 124 124 124 62 57 55 54 53 52 52 52 52 52 52 52 52 53
37 43 67 67 67 67 67 67 0 0 0 27 28 29 42 43 44 111 111 111 112 112 112 92 85
83 82 81 81 80 80 80 80 79 79 78 79 80 83 84 84 84 84 84 84 45 40 38 37 36 35
35 35 35 35 70 72 74 76 78 85 110 110 111 111 112 112 45 45 45 45 45 45 77 80
82 127 127 126 126 125 124 126 126 126 104 103 80 79 78 76 73 71 68 65 62 94
95 93 91 89 87 85 82 80 77 75 45 45 45 76 78 78 78 78 76 45 45 44 44 43 43 42
101 102 102 102 101 66 67 67 67 68 68 68 68 68 68 68 68 68 68 68 68 68 68 67
67 66 66 65 64 63 61 0 0 0
The largest value is the smallest height for the final 'smashed' together image.
Which is ===>
127
If images are not the same with you can try sliding the two sets of values to find a minimal result for the maximum value, as you slide the smaller image across the width of the larger.
use -extent on the first image to expand it to that hight, and overlay the second image using gravity south in the result.
Code: Select all
convert one.png -background none -extent x127 \
two.png -gravity south -composite smushed.png
If when you have the two sets of distance values you 'slide' the smaller image across. you may be able to find a smaller 'smush' of the two images where the 'y' falls between the 'ce'. But in general it works.
If you want to ensure some space between the images do a 'general thicken' of the alpha channel first (both vertically and horizontally). A blur and threshold may work better for this, but 'thichen' or a inverted distance function may also be used. The tools are their, you just need some other IM scripts or options to make use of them.
A -smush type option may be a good addition to IM, and and you can see can be done!
Now If I can just write this up some where (what area of IM example should it be in?)