Page 1 of 1
Croppign black border, improving speed
Posted: 2014-10-15T07:10:13-07:00
by jaffamuffin
Hi All
I have a bunch of scanned images, these are scanned from a book, and they are scanned left page and right page (i.e. not open book) So this means the left pages have a black border on top left and bottom and the right pages have border on top right and left.
the border is approx 1 inch at 300 dpi and i need to deskew and crop the border off. My code is below. It works but is there a way to combine the initial read with the crop as I am reading the image twice here and It is quite slow - I'm getting approx 1100 images an hour on a Z220 xeon e3125 workstation
Code: Select all
FOR /F %%A IN ('DIR %sourcedir%\*.jpg /B /S /A-D /ON') DO (
FOR /F %%B IN ('convert %%A -scale 5%% -threshold 128 -scale 2000%% -threshold 128 -bordercolor black -border 1x1 -fuzz 20%% -trim -format "%%wx%%h%%X%%Y" info:') DO (
convert %%A -deskew 60%% -crop %%B "A:%%~pnxA"
)
It's windows batch, essentially it's getting the crop region as %%B (e.g. 3040x3580+341+281 ) and feeding this to a crop and deskew command. (the output file "A:%%~pnxA" is a quick and dirty way to output the same file in the same file structure but I have changed the drive letter, and mapped my output directory to this letter A:)
Any ideas ?
I'm just about to split this into creating a listing of crop commands, and then running these commands on another PC but can I speed up the inherent process?
Thanks!
Re: Croppign black border, improving speed
Posted: 2014-10-15T12:06:23-07:00
by snibgo
You are getting crop parameters, but then doing a "-deskew" before applying that crop. So won't the crop parameters be wrong? The "-border 1" will also make the parameters out (by 1).
"-scale 2000%%" won't create the original size unless both dimensions were multiple of 20. So, again, the crop will be wrong (by up to 20).
"-format "%%@" calculates the trim but doesn't do it, so should be faster than "-trim -format "%%wx%%h%%X%%Y"
Does the second "-threshold 128" have any effect?
I think a single convert should work: scale, threshold, scale to exact original size, border, trim, deskew. But deskew before trim may be better.
Re: Croppign black border, improving speed
Posted: 2014-10-16T02:23:18-07:00
by jaffamuffin
snibgo wrote:You are getting crop parameters, but then doing a "-deskew" before applying that crop. So won't the crop parameters be wrong? The "-border 1" will also make the parameters out (by 1).
Ah yes the border will be adding the +1. I deskewed before the crop so that the rectangular page would hopefully more closely match the rectangular crop selection. But I can see that deskewing after is probably better (and quicker as less image data to deskew ? )
"-scale 2000%%" won't create the original size unless both dimensions were multiple of 20. So, again, the crop will be wrong (by up to 20).
Yeah that explains why all the crop dimensions end in 00,20,40,60 or 80. Should I resize to 5% and then back to original size ? Is that possible. To be honest I was willing to forgo some accuracy for the speed. The alternative is to perform a gaussian blur but this is relatively slow compared to a quick 20x reduction and enlarge. The crop doesn't have to be perfect, the results so far have been acceptable. 20px is less than a 10th of an inch.
"-format "%%@" calculates the trim but doesn't do it, so should be faster than "-trim -format "%%wx%%h%%X%%Y"
Nice tip. thanks
Does the second "-threshold 128" have any effect?
I thought after the rescale back up it's got grey in it againe, so threshold will make it black and white and make the trim work better.
I think a single convert should work: scale, threshold, scale to exact original size, border, trim, deskew. But deskew before trim may be better.
[/quote]
OK but how do I have a single command? The first command still spits out the trim dimensions and therefore i still need to crop the actual image ?
Re: Croppign black border, improving speed
Posted: 2014-10-22T03:37:50-07:00
by jaffamuffin
It seems that the command
Code: Select all
convert 0029.jpg -background black -deskew 60% -bordercolor Black -border 1 -fuzz 90% -trim c:\out.jpg
Will actually perform the required operation much faster , so this is what I will go with unless there is a good reason not to use trim ?
Do i need a +repage command ?
thanks
Re: Croppign black border, improving speed
Posted: 2014-10-22T03:56:46-07:00
by snibgo
"+repage" removes the offset. But this isn't stored in JPEG files, so "+repage" here would be redundant.
Re: Croppign black border, improving speed
Posted: 2014-10-22T04:33:39-07:00
by jaffamuffin
Good point. Thanks
Re: Croppign black border, improving speed
Posted: 2014-10-22T04:42:54-07:00
by jaffamuffin
Is there anyway to crop the image and spit out the new image size and/or crop region in one operation ? I say this because i had been writing the crop region to text file like this :
Code: Select all
R:\001.jpg,3220x4460+1+1
R:\002.jpg,3260x4260+301+201
R:\003.jpg,3220x4460+1+1
R:\004.jpg,3260x4260+301+201
R:\005.jpg,3220x4460+1+1
R:\006.jpg,3280x4280+281+201
And this show me that the right hand pages are not being cropped as much as the left hand (there's an extra white mark in the margin) which is good for quality checking so I can go and sort these out individually. Obviously the desire is to have pages of a similar height.
Re: Croppign black border, improving speed
Posted: 2014-10-22T05:17:06-07:00
by snibgo
You can "-write info:" before the output filename. If you don't like the standard info, precede this with a "-format", using any of the escapes from
http://www.imagemagick.org/script/escape.php