|3 of 24|
Welcome to the third 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.
So far we have requested information from images, But later in December we would like to change the images and would like to see this change when we put a filter on the image, for example. For this we need to be able to write the edited image which is in our working memory back to a file. How this works I will show you today. First we think about where we want to have our file. Do we want to write it into a file or into a container, for both we need again different functions.
Let's start with the case that we want to write the image into a file. For this we use the function GMImage.WriteToFile. This function writes a single image to the disk in the parameters we specify the image reference and the path to which the image should be saved. If you want to let the user choose in a dialog where to save the file you can use the functions from the FileDialog component. With these functions you can customize your save dialog by adding a text, a heading and many other settings. After selecting a file we can then get the file path and save the image with the GMImage.WriteToFile function to the specified location. In the script example we have used such a dialog.
Set Variable [ $GM ; Value: MBS("GMImage.NewFromContainer"; GraphicsMagick Advent::Image) ] Set Variable [ $r ; Value: MBS( "FileDialog.Reset" ) ] Set Variable [ $r ; Value: MBS( "FileDialog.SetWindowTitle"; "Save the image") ] Set Variable [ $r ; Value: MBS( "FileDialog.SetMessage"; "Where should the image be saved?" ) ] Set Variable [ $r ; Value: MBS( "FileDialog.SaveFileDialog" ) ] Set Variable [ $Path ; Value: MBS("FileDialog.GetPath"; 0) ] Set Variable [ $Path ; Value: $Path & ".png" ] Set Variable [ $r ; Value: MBS( "GMImage.WriteToFile"; $GM; $Path ) ] Set Variable [ $r ; Value: MBS( "GMImage.Release"; $GM ) ]
We have our image reference in the variable $GM. First, we reset all the existing settings of the dialog so that we don't take any legacy with us. The dialog gets a window title with the text "Save the image"" and the message "Where should the image be saved?"
We have decided to use the save dialog because we want to write a file to the disk and so we only adapt the standard save dialog from the operating system to our wishes. For this we use the function FileDialog.SaveFileDialog. We get the path we selected with the function FileDialog.GetPath. Since you can also select multiple files directly with a file dialog we have to specify an index with this function to determine how many paths we want. We want the first one and for this reason we specify 0, because in the MBS FileMaker Plugin we start counting indexes at 0. We still need to specify what type our file should be, otherwise a text file will be written with the image. For file path we simply append the sufix, here png. Now we can use the function to write it to the disk and of course release the reference again.
If we have several references at the same time, we can also write them directly to the disk in one step. For this we use the GMImage.WriteImages function. In the parameters we specify a list with the references, then again the path to which we want to save the images. Last but not least we can decide if we want to save the images as single files or if we want to write the images into a multi image tiff file.
We can also put an image directly into a container. We use the GMImage.WriteToContainer function. In the parameters we first specify the reference and then the file name. With this function we can choose before which format our image should have. For this we use GMImage.SetMagick before this function. Here we also specify the reference and the type.
Set Variable [ $GM ; Value: MBS("GMImage.NewFromContainer"; GraphicsMagick Advent::Image) ] Set Variable [ $r ; Value: MBS( "GMImage.SetMagick"; $GM; "PNG") ] Set Field [ GraphicsMagick Advent::Image ; MBS( "GMImage.WriteToContainer"; $GM ; "abc.png" ) ]
If you already know in detail which file format your image should have in the container, then you can use one of our special functions that we offer for some file formats. The functions have the same parameters as the main function. Here is a list of these special functions:
We'll be using the functions we learned about today often during the Advent calendar. I hope you enjoyed the third door and I will see you here again tomorrow for door number four.