|23 of 24|
Welcome to the 23th 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'm going to show you how to reduce the size of your image files. If you want to store images in your database, images often consume a lot of space. But sometimes the images don't have to be that huge, because you only want to display them in a container.
On one hand, if available, you can use the function "GMImage.ExifThumbnail" to retrieve the thumbnail and write it to the database instead of the original image. But with many images the thumbnail cannot be retrieved easily and an error occurs or we get back an empty value. So this case we should catch for sure.
... Set Variable [ $IMG ; Value: MBS( "GMImage.ExifThumbnail"; $GM; "abc.jpg") ] If [ MBS("IsError") or $IMG="" ] Show Custom Dialog [ "Error" ; "Thumbnail cannot be retrieved¶" & $IMG ] Set Variable [ $r ; Value: MBS( "GMImage.ReleaseAll" ) ] Exit Script [ Text Result: ] End If Set Field [ GraphicsMagick Advent::Image ; $IMG ] ...
Of course, we always have the possibility to scale our image, which means to change its physical size. How this works in detail we have seen in door 5.
But we don't necessarily have to change the physical size to make our file smaller. For example, you can save an image as grayscale instead of RGB. This has the advantage that the color information in the grayscale is more compact. In an RGB pixel we have to store 24 bits of information (8 bit red, 8 bit green and 8 bit blue) for each pixel. In grayscale it is 8 bit. For this reason, the size is drastically reduced when we display the image in grayscale.
... Set Variable [ $r ; Value: MBS( "GMImage.SetType"; $GM; 3 ) ] ...
You can find more information about this function in door 8. Note that not every image that looks gray is also saved in the grayscale color space. Even in the RGB color space, an image can only have colors that are gray. So if you get an image from a scanner, don't count on the image being in grayscale format. You can also convert the image directly to black and white instead of grayscale format, which also saves memory. For this you use the same function as for grayscale, only this time you put a 1 in the parameters instead of the 3. This formats the image to black and white
Set Variable [ $r ; Value: MBS( "GMImage.SetType"; $GM; 1 ) ]
Another thing that can reduce the size of the image without having to physically reduce the size of the image and without having to sacrifice color is setting the quality of a JPEG image. A JPEG image has a built-in compression and we can now set the level of compression via the quality. For this we use GMImage.SetQuality function to set the compression level. If we don't use this function before we save the image as JPEG the default value is 75. Here you can see that we set the compression level to 50 before we write the image as JPEG back into the container.
Set Variable [ $GM ; Value: MBS("GMImage.NewFromContainer"; GraphicsMagick Advent::Image) ] Set Variable [ $r ; Value: MBS( "GMImage.SetQuality"; $GM; 50 ) ] Set Field [ GraphicsMagick Advent::Image ; MBS( "GMImage.WriteToJPEGContainer"; $GM ) ] Set Variable [ $r ; Value: MBS( "GMImage.Release"; $GM ) ]
These reduction mechanisms can also be combined with each other. You can see this for example here with the image. First we reduced the physical size, then the image was converted into a grayscale image and then with a quality of 50 converted into a JPEG image.
Here you must be careful that sometimes the mechanisms do not work well together, for example, if you convert a JPEG image into a black and white image, the file size may even increase.
I hope you liked this door and it helps you. Be happy to join us tomorrow when we open the last door where the whole thing becomes round.