Remove Blank pages from Multi Page Tiff
-
- Posts: 6
- Joined: 2012-02-15T08:51:20-07:00
- Authentication code: 8675308
Remove Blank pages from Multi Page Tiff
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
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove Blank pages from Multi Page Tiff
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
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
Last edited by fmw42 on 2012-02-15T13:08:54-07:00, edited 2 times in total.
-
- Posts: 6
- Joined: 2012-02-15T08:51:20-07:00
- Authentication code: 8675308
Re: Remove Blank pages from Multi Page Tiff
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]
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]
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove Blank pages from Multi Page Tiff
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
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
-
- Posts: 6
- Joined: 2012-02-15T08:51:20-07:00
- Authentication code: 8675308
Re: Remove Blank pages from Multi Page Tiff
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
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
-
- Posts: 6
- Joined: 2012-02-15T08:51:20-07:00
- Authentication code: 8675308
Re: Remove Blank pages from Multi Page Tiff
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
How can I get the command to show me the mean value
Thanks
Scott
-
- Posts: 6
- Joined: 2012-02-15T08:51:20-07:00
- Authentication code: 8675308
Re: Remove Blank pages from Multi Page Tiff
Nevermind I figured out how to display the value of fx:mean
convert Inv__5675671_1_page1.tif -format "%[fx:mean]" info
convert Inv__5675671_1_page1.tif -format "%[fx:mean]" info
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Remove Blank pages from Multi Page Tiff
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
http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/Usage/transform/#fx_escapes
http://www.imagemagick.org/script/fx.php
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Remove Blank pages from Multi Page Tiff
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..
and look for 1x1 images!
Once you know what frames to use a second command to delete them from the original.
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:
Once you know what frames to use a second command to delete them from the original.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 6
- Joined: 2012-02-15T08:51:20-07:00
- Authentication code: 8675308
Re: Remove Blank pages from Multi Page Tiff
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.
Thanks again for your help
Scott
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())"
Scott