Interesting Problem...Overlap only the transparent portions
Interesting Problem...Overlap only the transparent portions
I'm trying to overlay two images, top to bottom, but with some overlap of the transparent areas.
Image #1: convert -background none -fill red -font Arial -pointsize 72 label:Jimmy\'s one.png
Image #2: convert -background none -fill blue -font Courier -pointsize 72 label:Place two.png
I found some help that showed me how to concatenate the two images top to bottom, as close as possible without overlapping:
Initial Result: convert -background none -append -alpha set -bordercolor none -trim -layers trim-bounds one.png two.png three.png
However, what I'm trying to do is to get those two lines of text much closer together. With the command above, the 'y' in 'Jimmy's' sets the bottom border of the first image, and puts too much space between the two lines of text. I'm trying to get something like this "hand created" image:
Is there anything in ImageMagick that could overlay two images, top to bottom, and overlap the transparent areas without allowing any colored pixels to overlap? Would be stellar if I could specify some number of pixels as a "border"... ( i.e, keep colored pixels in image #1 at least X pixels away from colored pixels in image #2 ).
Note: In the production code, these images are generated on the fly from user generated text, fonts, etc, so I can't just overlap the images manually using some pixel offset.
FYI, thanks so much to everyone that works on IM. It's a wonderful and powerful tool!
Image #1: convert -background none -fill red -font Arial -pointsize 72 label:Jimmy\'s one.png
Image #2: convert -background none -fill blue -font Courier -pointsize 72 label:Place two.png
I found some help that showed me how to concatenate the two images top to bottom, as close as possible without overlapping:
Initial Result: convert -background none -append -alpha set -bordercolor none -trim -layers trim-bounds one.png two.png three.png
However, what I'm trying to do is to get those two lines of text much closer together. With the command above, the 'y' in 'Jimmy's' sets the bottom border of the first image, and puts too much space between the two lines of text. I'm trying to get something like this "hand created" image:
Is there anything in ImageMagick that could overlay two images, top to bottom, and overlap the transparent areas without allowing any colored pixels to overlap? Would be stellar if I could specify some number of pixels as a "border"... ( i.e, keep colored pixels in image #1 at least X pixels away from colored pixels in image #2 ).
Note: In the production code, these images are generated on the fly from user generated text, fonts, etc, so I can't just overlap the images manually using some pixel offset.
FYI, thanks so much to everyone that works on IM. It's a wonderful and powerful tool!
Re: Interesting Problem...Overlap only the transparent porti
Okay...seems nobody has anything easy.
How about a general pointer? The -layers command looks like it might be able to be coaxed into showing me when I've overlaid the images too closely. Any way to get it to show me just non-transparent pixels that changed?
How about a general pointer? The -layers command looks like it might be able to be coaxed into showing me when I've overlaid the images too closely. Any way to get it to show me just non-transparent pixels that changed?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Interesting Problem...Overlap only the transparent porti
This is just a concept. I don't know how easy it will be nor how effective.
Take the two fixed images of the text and extract the binary alpha channel mask image (white where letters, black for background). Then composite them each over a large common sized black background image at various positions. Get the mean of the result. When the two text images don't overlap, the mean will be maximum value. When they overlap, the mean will be smaller. You can get the mean value of an image from
convert image -format "%[mean]" info:
You want to keep moving the two mask images closer and closer in Y horizontally and in X until they overlap some. Then take the value just before the mean gets reduced and for which the distance between the two image's Y values are closest (smallest).
Take the two fixed images of the text and extract the binary alpha channel mask image (white where letters, black for background). Then composite them each over a large common sized black background image at various positions. Get the mean of the result. When the two text images don't overlap, the mean will be maximum value. When they overlap, the mean will be smaller. You can get the mean value of an image from
convert image -format "%[mean]" info:
You want to keep moving the two mask images closer and closer in Y horizontally and in X until they overlap some. Then take the value just before the mean gets reduced and for which the distance between the two image's Y values are closest (smallest).
Re: Interesting Problem...Overlap only the transparent porti
Terrific! Thanks....that gives me something to play with.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Interesting Problem...Overlap only the transparent porti
Another approach might be to use the compare function. Place the first alpha channel in a suitably large black background image, large enough to hold both text images well separated. The use the compare function to match the second (smaller) text image to the background image with the first text image in place. Then look at the similarity image result to see where the image is totally black (graylevel 0), but very close to where it just is slightly larger than graylevel 0 in the region near where you might want it to be placed.
As a reference to using compare with a large and small image, see
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076
but in newer version of IM, you will need to add -submimage-search to the command line when the two images are not the same size.
As a reference to using compare with a large and small image, see
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076
but in newer version of IM, you will need to add -submimage-search to the command line when the two images are not the same size.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Interesting Problem...Overlap only the transparent porti
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.
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.
Now add up the columns by using a DIY distance function and extract result.
Add the column numbers of the two images. (you could skip the previous steps, if they are the same width)
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.
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?)
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:
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:
The largest value is the smallest height for the final 'smashed' together image.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
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?)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Interesting Problem...Overlap only the transparent porti
NIce solution, Anthony. Link it both ways between text processing and morphology.
My compare solution above is flawed where white and black overlap. Where white overlaps black the rmse metric will get higher and where white overlaps white it will get smaller. Compared to when the two images are separate. But it might work better if one had white and transparent images.
I am curious about whether compare can be used with white text on a transparent background. Is compare sensitive to transparency such that it only produces a difference in the compare metric results where white overlaps white and does not change the metric value where white and transparent overlap?
If that worked, then once could get the similarity image and threshold it and then get an edge image and then output that to txt format and look at the coordinates for optional places to put the second image.
My compare solution above is flawed where white and black overlap. Where white overlaps black the rmse metric will get higher and where white overlaps white it will get smaller. Compared to when the two images are separate. But it might work better if one had white and transparent images.
I am curious about whether compare can be used with white text on a transparent background. Is compare sensitive to transparency such that it only produces a difference in the compare metric results where white overlaps white and does not change the metric value where white and transparent overlap?
If that worked, then once could get the similarity image and threshold it and then get an edge image and then output that to txt format and look at the coordinates for optional places to put the second image.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Interesting Problem...Overlap only the transparent porti
For your solution I would level the alpha masks to a 50% value and then look for peaks that were larger than 50%. However it would all be a 2 dimentional peak search.
The 'smush' method I presented only does a 1 dimensional compare, and you can slide a smaller image along to find the best smush when images are of unequal width.
It will also roughly fit two complementary shapes together, as long as you don't have any jigsaw like key holes in the edge.
However I have no idea where I should pot this example! Probably in advanced as it uses a number of techniques.
The 'smush' method I presented only does a 1 dimensional compare, and you can slide a smaller image along to find the best smush when images are of unequal width.
It will also roughly fit two complementary shapes together, as long as you don't have any jigsaw like key holes in the edge.
However I have no idea where I should pot this example! Probably in advanced as it uses a number of techniques.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Interesting Problem...Overlap only the transparent porti
Advanced sounds reasonable for where the example goes. But I would also suggest a link to it from both Morphology and Text as it regards both.Anthony wrote:However I have no idea where I should pot this example! Probably in advanced as it uses a number of techniques.
But you did not answer my question about how compare handles transparency, if you know.
Fred
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Interesting Problem...Overlap only the transparent porti
In general Compare treats transparency as just another dimension.
That however means fully-transparent white does not equal fully-transparent black.
That is A channel is treated the same as a color channel, even though it is not.
As such before comparing images with full transparency, you may need to use -alpha background to make all fully-transparent colors the same, though 'distance results' may not be right.
This was one of my problems in comparing colors (with fuzz)
http://www.imagemagick.org/Usage/quantize/#compare
By the way the new compare -metric fuzz did not appear to produce a correct fuzz value. I did report this as a bug, but have not checked to see if it was fixed.
That however means fully-transparent white does not equal fully-transparent black.
That is A channel is treated the same as a color channel, even though it is not.
As such before comparing images with full transparency, you may need to use -alpha background to make all fully-transparent colors the same, though 'distance results' may not be right.
This was one of my problems in comparing colors (with fuzz)
http://www.imagemagick.org/Usage/quantize/#compare
By the way the new compare -metric fuzz did not appear to produce a correct fuzz value. I did report this as a bug, but have not checked to see if it was fixed.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Interesting Problem...Overlap only the transparent porti
Try ImageMagick 6.6.7-7 Beta. It has a new smush option, for example:
- convert top.png bottom.png -background none -gravity center -smush 2 smush.png