« MBS Plugin Preference… | Home | MBS @ FMTraining.TV -… »

GraphicsMagick in FileMaker, part 22

🎄
22 of 24

Welcome to the 22th door of our advent calendar. In this advent calendar I would like to take you on a journey through the GraphicsMagick component in December. Every day I will introduce you to one or more functions from this component. In this component you will find functions with which you can analyze images, convert them, change them with filters, draw them and much much more. In the end, you too can take the magic of GraphicsMagick to your images. I wish you a lot of fun in the process.

Today I show you how you can combine pictures. It's almost Christmas and our monkey is still missing the right costume. This we want to put on him today. The Christmas hat is on a separate PNG image. That means we want to put the image with the Christmas hat on top of the image with our logo. For this we can choose from two functions GMImage.Composite and GMImage.CompositeXY. I would like to introduce the GMImage.Composite function first. First we specify the reference in which we want to have the result in our case this is the reference of the logo. Next is the reference of the image or workspace we want to combine with the image, in our case an image of the cap. The next parameter refers to how the second image is arranged to the first one. Our two images are the same size, so it makes no difference for us the way the two images are positioned in relation to each other, but if the images are different in size, for example the second image is smaller than the first, then it makes a difference whether we align the image to the upper left corner or centered. The following options are available to us:

  • ForgetGravity=0
  • NorthWestGravity=1
  • NorthGravity=2
  • NorthEastGravity=3
  • WestGravity=4
  • CenterGravity=5
  • EastGravity=6
  • SouthWestGravity=7
  • SouthGravity=8
  • SouthEastGravity=9
  • StaticGravity=10

Here we see on the small box we put over the logo how the Gravity behaves:
CenterGravity=5

NorthWestGravity=1

Optionally, we can now specify how the two images should be combined. We can choose from the following options:

  • UndefinedCompositeOp = 0
  • OverCompositeOp = 1
  • InCompositeOp = 2
  • OutCompositeOp = 3
  • AtopCompositeOp = 4
  • XorCompositeOp = 5
  • PlusCompositeOp = 6
  • MinusCompositeOp = 7
  • AddCompositeOp = 8
  • SubtractCompositeOp = 9
  • DifferenceCompositeOp = 10
  • MultiplyCompositeOp = 11
  • BumpmapCompositeOp = 12
  • CopyCompositeOp = 13
  • CopyRedCompositeOp = 14
  • CopyGreenCompositeOp = 15
  • CopyBlueCompositeOp = 16
  • CopyOpacityCompositeOp = 17
  • ClearCompositeOp = 18
  • DissolveCompositeOp = 19
  • DisplaceCompositeOp = 20
  • ModulateCompositeOp = 21
  • ThresholdCompositeOp = 22
  • NoCompositeOp = 23
  • DarkenCompositeOp = 24
  • LightenCompositeOp = 25
  • HueCompositeOp = 26
  • SaturateCompositeOp = 27
  • ColorizeCompositeOp = 28
  • LuminizeCompositeOp = 29
  • CopyCyanCompositeOp = 32
  • CopyMagentaCompositeOp = 33
  • CopyYellowCompositeOp = 34
  • CopyBlackCompositeOp = 35
  • DivideCompositeOp = 36

We will not look at all of these options today, but if you have a general interest in how the other options work, please take a look at the Combine Pictures example. The following pictures are also taken from this example

Let's start with the default value. This is InCompositeOp (2). With this composition we put the second image on top of the first one. The alpha channel information of image 2 is completely ignored and interpreted as white.

With LightenCompositeOp (27) we get the effect of a watermark. The image background becomes lighter in the places where there is color on the superimposed image, depending on the color value.

The situation is similar to DarkenCompositeOp. Here it is darkened depending on the color.

The most frequently used option is probably OverCompositeOp (1) This option applies image two to image 1, taking care of the alpha channel information. We also use this option to put the cap on our monkey. Our script looks like this:

Set Variable [ $GMLogo ; Value: MBS("GMImage.NewFromContainer"; GraphicsMagick Advent::Logo) ] 
Set Variable [ $GMCap ; Value: MBS("GMImage.NewFromContainer"; GraphicsMagick Advent::Cap) ] 
Set Variable [ $r ; Value: MBS( "GMImage.Composite";$GMLogo; $GMCap; 1; 1 ) ] 
# 
Set Field [ GraphicsMagick Advent::Image ; MBS("GMImage.WriteToPNGContainer"; $GMLogo; "abc.png") ] 
Set Variable [ $r ; Value: MBS( "GMImage.ReleaseAll" ) ] 

So now our result image looks like this:

We have just said that in addition to the GMImage.Composite function we can also use the GMImage.CompositeXY. Here, image2 will be placed on image1 with the help of an offset, specifying X and Y. So we don't need to specify the Gravity we used in the GMImage.Composite function. Instead we specify how we want to move image 2 in relation to the upper left corner of image 1. Our monkey still has some hair coming out of the top of the hat, we want to change this by moving the image in negative Y direction by 5 pixels. By the way, nothing changes in the overlay options, they are the same. Here we see the appropriate code:

Set Variable [ $GMLogo ; Value: MBS("GMImage.NewFromContainer"; GraphicsMagick Advent::Logo) ] 
Set Variable [ $GMCap ; Value: MBS("GMImage.NewFromContainer"; GraphicsMagick Advent::Cap) ] 

Set Variable [ $r ; Value: MBS( "GMImage.CompositeXY"; $GMLogo; $GMCap; 0; -5  ; 1  ) ] 

Set Field [ GraphicsMagick Advent::Image ; MBS("GMImage.WriteToPNGContainer"; $GMLogo; "abc.png") ] 
Set Variable [ $r ; Value: MBS( "GMImage.ReleaseAll" ) ] 

Now the monkey's cap also fits perfectly.

I hope you liked the door and see you tomorrow.

Previous day   Next day

22 12 22 - 08:50