Page 1 of 1

Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T09:19:23-07:00
by sstanaland
Hi

I'm using the convert command to convert multi page tiffs into pdf files.

This is working well, but if possible I would like to find a command that will remove blank pages (mostly blank).

I believe our scanner is supposed to suppress blank pages, but I have tiff images with blank pages for the back of each page.

There are just a few specs on the blank pages.

Is there a way to accomplish this with imagemagick?

Thanks
Scott

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T11:44:14-07:00
by fmw42
You would have to write a script to separate each page test if nearly pure white (I presume not black) and skip if too white and write only those files to pdf. You probably want to save the files that pass the test to something like miff as it stores multi-page images and then once all are collected, then write to pdf.

What version of IM are you using and more importantly what platform. If unix, I can probably work up a script.

The key is to test for the mean of the image and if too close to 1 (white), then skip. The test in unix/IM is

[ loop over all pages ]
threshold=0.9 # 90% white
test=`convert image -format "%[fx:mean>$threshold?1:0]" info:`
if [ $test -ne 1 ]; then
[write/save your page]
[end loop]

You can then adjust your threshold as desired.

I am not a windows user so you are then you should see http://www.imagemagick.org/Usage/windows/ for syntax and DOS scripting

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T12:34:05-07:00
by sstanaland
Thanks for your reply.

I am a windows user. Version is ImageMagick 6.7.5-7 2012-02-13 Q16.

I will take a look at your example and see if I can come up with something.

Thanks

[quote][/quote]

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T13:02:24-07:00
by fmw42
This works in unix for a tiff file with 4 pages, one of them white

http://www.fmwconcepts.com/misc_tests/test.tiff

infile="test.tiff"
inname=`convert $infile -format "%t" info: | head -n 1`
outfile="${inname}.pdf"
threshold=0.9
convert ${infile}[0] $outfile
for ((i=0; i<4; i++)); do
test=`convert $infile[$i] -format "%[fx:mean>$threshold?1:0]" info:`
if [ $test -ne 1 ]; then
convert $outfile $infile[$i] $outfile
fi
done
convert $outfile -delete 0 $outfile




This also should work, but my Mac system seems to be having trouble with subshells for some odd reason. Sometimes it works and sometimes it errors out trying to find some IM tmp file???

infile="test.tiff"
inname=`convert $infile -format "%t" info: | head -n 1`
outfile="${inname}.pdf"
threshold=0.9
( for ((i=0; i<4; i++)); do
test=`convert $infile[$i] -format "%[fx:mean>$threshold?1:0]" info:`
if [ $test -ne 1 ]; then
convert $infile[$i] miff:-
fi
done ) | convert - $outfile

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T13:55:11-07:00
by sstanaland
I'm trying on a single image after I split them up from the command line for testing

I'm running the following command, and I always get a 1 back on a full and on a mostly white page.
convert Inv__5675671_1_page1.tif -format "%[fx:mean>0.9?1:0]" info:

Do you see any problem with my command?

Thanks

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T14:11:37-07:00
by sstanaland
I actually get a differenct result if I change the threshhold value to 0.99

How can I get the command to show me the mean value

Thanks
Scott

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T14:16:39-07:00
by sstanaland
Nevermind I figured out how to display the value of fx:mean

convert Inv__5675671_1_page1.tif -format "%[fx:mean]" info

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T15:00:00-07:00
by fmw42
Sorry, I should have explained a bit further. The test is a ternary command. It sets the output to 1 if the condition is met and 0 otherwise. Glad you figured it out. see

http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/Usage/transform/#fx_escapes
http://www.imagemagick.org/script/fx.php

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-15T23:26:58-07:00
by anthony
One method if you do not mind 'trimming' your pages is to 'trim' and then remove 'null images'
Trim creates 'null images' when the whole image is trimmed, it is a 1x1 pixel image at a layers offset of -1-1

Damn... I never implemented "-layers RemoveNull" ! I meant to :(

Well it will make finding such frames for removal easy do this is it..

Code: Select all

  convert images.tif  -bordercolor white -border 1x1 -fuzz 5% -trim info:
and look for 1x1 images!

Once you know what frames to use a second command to delete them from the original.

Re: Remove Blank pages from Multi Page Tiff

Posted: 2012-02-16T14:49:04-07:00
by sstanaland
Hi Guys,

I was able to get this working, thanks to all your help.

I'm using the following command to split the tiff into single page tiffs:
convert image.tif image_page%d.tif

This command to check for mostly white space:
convert image_page5.tif -format "%[fx:mean>0.99?1:0]" info:

And this command to join the single page tiffs into one pdf:
convert -adjoin *_page*.tif pdf:all_these_files_in_one.pdf

Here's a windows powershell script I wrote to automate the process.

Code: Select all

$ElapsedTime = [System.Diagnostics.Stopwatch]::StartNew()

$InputTif = $args[0]
$OutputPDF = $args[1]

$WorkPath = ".\work"
$ImageMagickPath = "C:\Program Files (x86)\ImageMagick-6.7.5-Q16"

if (Test-Path -path $InputTif) # Check if input tif exist
{
	# Create workdir if not already there
	if (!(Test-Path -path $WorkPath))
	 {
		New-Item $WorkPath -type directory
	 }
	 
	 $InputFileName = split-path $InputTif -Leaf

	#SAMPLE COMMAND:  convert c:\Inv__5675671_1.tif Inv__5675671_1_page%d.tif
	 
	# Split image into single page tiff's in a work directory
	$cmd = "& ""$ImageMagickPath\convert"" ""$InputTif"" " + """$WorkPath\$InputFileName" + "_page%d.tif"""
	invoke-expression $cmd 

	#Loop through single page tiff's and delete pages that are mostly blank
	foreach($fil in (get-childitem $WorkPath\*.tif))
	{
		#SAMPLE COMMAND: convert Inv__5675671_1_page5.tif -format "%[fx:mean>0.99?1:0]" info:
		
		$cmd = "& ""$ImageMagickPath\convert"" ""$fil"" " + "-format ""%[fx:mean>0.99?1:0]"" info:"

		$isblank = invoke-expression $cmd 
		
		if ($isblank -ne 0)
		{
			remove-item $fil.fullname
		}
	}
	
	#Join tiff into single output pdf file
	#SAMPLE COMMAND: convert -adjoin *_page*.tif pdf:all_these_files_in_one.pdf

	$cmd = "& ""$ImageMagickPath\convert"" -adjoin $WorkPath\*_page*.tif $OutputPDF"
	Invoke-expression $cmd 	
	
	#Clear out workdir
	foreach($fil in (get-childitem $WorkPath\*.tif))
	{
		remove-item $fil.fullname
	}
	
	write-host "Finished!!!"
}	
else
{
"Input Tiff does not exist"
}

write-host "Total Elapsed Time: $($ElapsedTime.Elapsed.ToString())"
Thanks again for your help

Scott