|9 of 24|
Welcome to the 9th 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 tell you a little bit about a function that is quite inconspicuous, but once provided me with great services. I got from a customer at the very beginning of my FileMaker time the order that I should sort out all pages from a scanned PDF file that are empty. I accepted the order and thought, that can't be difficult, there must be a simple function you can use to detect if the page is empty or not. What can I say it wasn't that easy and I was thinking for a long time about how to do it until I found the GMImage.AveragePixelValue function. This function gets the average color value of pixels in an image or a certain area which can be defined with optional parameters in the function.
... Set Variable [ $AveragePixelValue ; Value: MBS( "GMImage.AveragePixelValue"; $GM; 0; 0; $imageWidth; $imageHeight ) ] If [ $AveragePixelValue < ,99 ] Set Variable [ $r ; Value: MBS( "DynaPDF.AppendImagePages"; $pdf; $currentPicturePath ) ] ...
Before inserting the pages as images into a PDF with DynaPDF, the images of the pages scanned by the scanner were available as container values, so I could simply load them into memory as reference. Of course you can use the GMImage.Threshold function again to convert an image to a black and white copy. The GMImage.AveragePixelValue function returns a value between 0 and 1 that tells us if the image is more or less black or white. Of course, if you scan with a normal scanner, not everything is white when the page is blank. It could be a bend of the paper, it could be because the paper had a yellow tone and so some pixels are interpreted as black pixels. So we have to define a limit from when we say the page is blank or the page is printed. The best way to find such a threshold is to look at the values of scanned sample pages. The more white is on the page the higher is the value. I chose a value of 0.99, which still allows some wrinkles, but if there is a certain amount of text on the page, the page will not be sorted out.
Value = 0.9443
Value = 0.9987
After I finished this check the image was added to a DynaPDF document with DynaPDF.AppendImagePages. When using the GMImage.AveragePixelValue function you have to make sure that the image is in RGB format. For this reason it is recommended to use the GMImage.SetType function which is passed the type 6 for TrueColorType in the parameters. We have already seen the use of this function yesterday in connection with a grayscale image.
I hope the function can help you, as it helped me back then. I would be happy if we meet again tomorrow and make you text confident.