So I often run huge double-sided scan jobs on an unintelligent Canon multifunction, which leaves me with a huge folder of JPEGs. Am I insane to consider using PIL to analyze a folder of images to detect scans of blank pages and flag them for deletion?
Leaving the folder-crawling and flagging parts out, I imagine this would look something like:
- Check if the image is greyscale, as this is presumed uncertain.
- If so, detect the dominant range of shades (background colour).
- If not, detect the dominant range of shades, restricting to light greys.
- Determine what percentage of the entire image is composed of said shades.
- Try to find a threshold that adequately detects pages with type or writing or imagery.
- Perhaps test fragments of the image at a time to increase accuracy of threshold.
I know this is sort of an edge case, but can anyone with PIL experience lend some pointers?
Just as a first try, sort your image folder by file size. If all scans from one document have the same resolution the blank pages will certainly result in smaller files than the non-blank ones.
I don't know how many pages you are scanning, but if the number is low enough this could be a simple quick fix.
Here is an alternative solution, using mahotas and milk.
positives/
andnegatives/
where you will manually pick out a few examples.unlabeled/
directoryIn the code below I used jug to give you the possibility of running it on multiple processors, but the code also works if you remove every line which mentions
TaskGenerator
This uses texture features, which is probably good enough, but you can play with other features in
mahotas.features
if you'd like (or trymahotas.surf
, but that gets more complicated). In general, I have found it hard to do classification with the sort of hard thresholds you are looking for unless the scanning is very controlled.A few non-PIL-specific suggestions to consider:
Scans of printed or written material will have lots of high-contrast sharp edges; something like a median filter (to reduce noise) followed by some kind of simple edge detection might do a good job of discriminating real content from blank pages.
Testing fragments at a time is useful not only because it might increase your accuracy, but because it might help you to give up early on many pages. Presumably most of your scans are not blank, so you should begin with a simple-minded check that usually identifies non-blank pages as non-blank; only if it says the page might be blank do you need to look more closely.
In case either the illumination or the page itself is nonuniform, you might want to begin by doing something like
image = image-filter(image)
wherefilter
does a very broad smoothing of some kind. That will reduce the need to identify the dominant shades, as well as coping when the dominant shade isn't quite uniform across the page.