Page 2 of 3
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-09T13:21:01-07:00
by Adam32
...
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-09T13:23:00-07:00
by Adam32
Thanks for all your help. I just have a couple more questions
I am following these steps and it is working well:
Code: Select all
%IM%convert ^
punch_holes_Page_2.jpg ^
-threshold 50%% ^
-define connected-components:verbose=true ^
-define connected-components:area-threshold=190 ^
-define connected-components:mean-color=true ^
-connected-components 8 ^
t2.png
Code: Select all
%IM%convert ^
punch_holes_Page_2.jpg ^
( t2.png -crop 10%%x100%%+0+0 ^
-morphology Erode disk:3 ^
) ^
-alpha off ^
-set option:compose:outside-overlay false ^
-compose CopyOpacity -composite ^
h.png
Code: Select all
call %PICTBAT%blurFill h.png . h_bf.png
1) But how do I get it to also search the right margin as well as the left, and if it finds punch holes on the Right, fill those as well? as sometimes there are punch holes on the right hand side.
2) Is the code able to work with scanned documents that have been saved to a multi page PDF file? I have tried this with a PDF file but it did not work. I suppose I will have to extract all the images from the PDF, run the code and reassemble the PDF? Or is there a quicker way of doing this?
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-09T14:24:39-07:00
by snibgo
1. There are many methods. You can simply repeat the middle command, with "-gravity East" before the crop. The output of the first of these would be the input to the second.
2. I would extract the images (with pdfimages), apply the processing in a loop through the images, and reassemble.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-09T17:07:28-07:00
by Adam32
snibgo wrote:1. There are many methods. You can simply repeat the middle command, with "-gravity East" before the crop. The output of the first of these would be the input to the second.
If I do this it takes about 10 minutes to output h.png:
Code: Select all
%IM%convert ^
punch_holes_Page_2.jpg ^
( t2.png -gravity East -crop 10%%x100%%+0+0 ^
-morphology Erode disk:3 ^
) ^
-alpha off ^
-set option:compose:outside-overlay false ^
-compose CopyOpacity -composite ^
h.png
When I do the final stage:
Code: Select all
call %PICTBAT%blurFill h.png . h_bf.png
This takes about 1 hour for one file.
If the holes are on the left side, it only takes a few seconds. Why is the Right causing so much trouble and the left is very quick? Is there any other code I can try to remove holes which may appear on the left or right?
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-09T23:04:43-07:00
by snibgo
It works fine for me: 0.45 seconds for the convert, 1.91 seconds for the blurFill.
Check that your t2.png is correct. It should be mostly white, but black for the holes and some text where the letters toush each other.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-10T09:29:39-07:00
by Adam32
Something is going wrong when I try and do the right side. I have tried many files as an example for the right, but they always go wrong. The left works perfectly every time.
I think it generates a wrong t2.png:
Also here is the h.png:
I repeat that the left always works perfect.
There is also some other odd behaviour when I do the final stage (blurFill.bat h.png . h_bf.png). This will not work if the target images are in a folder name that contains more than 1 word with spaces. Also when I do the last stage I always get an error message saying "The system cannot find the path specified", but in the case of doing the left side things continue as normal and generate a correct file with the punch holes removed. For the right side things often just hang.
I have uninstalled imagemagick and reinstalled as I wondered if there may have been something wrong with my install, but that seems okay.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-10T09:59:20-07:00
by snibgo
Your t2.png is clearly wrong. Please post an input image that creates the problem, and the exact command you use. Do you run in from the console, or from a BAT file?
I never have spaces in my filenames or directory names. That will cause problems in my scripts. If you put quotes around filenames in the script "like this" they will work.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-10T11:52:20-07:00
by Adam32
As requested -
Here is the starting image that caused the problem:
This is the exact code I type in 3 stages:
Code: Select all
convert ^ 1.jpg ^ -threshold 50%% ^ -define connected-components:verbose=true ^ -define connected-components:area-threshold=190 ^ -define connected-components:mean-color=true ^ -connected-components 8 ^ t2.png
Code: Select all
convert ^ 1.jpg ^ ( t2.png -gravity East -crop 10%%x100%%+0+0 ^ -morphology Erode disk:3 ^ ) ^ -alpha off ^ -set option:compose:outside-overlay false ^ -compose CopyOpacity -composite ^ h.png
I have tried typing the code direct into the console. I have also tried saving the code as bat files and double clicking them. Both times I get the same problem when trying to remove punch holes if they are on the right.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-10T12:33:41-07:00
by snibgo
Your "left" page was 1241x1778 pixels. This "right" page is 9918x14062 pixels. That's about 63 times as large.
So processing will take about 63 times as long.
I gave a lower limit on the size of components as 190 pixels. As this image is 63 times as large, you should use 190*63 = 11970.
I gave you the syntax for Windows BAT. The caret "^" at the end of each line means the command continues on the next line. So if you put the entire command on one line, don't include these carets.
And, as I've said:
snibgo wrote:All the commands I gave are for a BAT script. If you type them at the console, change every doubled %% to a single %.
After thresholding the image to 50%, the connected-components verbose gives the first few lines:
Objects (id: bounding-box centroid area mean-color):
0: 9918x14062+0+0 4957.2,7032.5 139110389 srgb(33,33,33)
2558: 407x117+3364+8606 3570.6,8656.9 28089 srgb(0,0,0)
2451: 409x118+6594+8006 6806.5,8058.5 27865 srgb(0,0,0)
2405: 102x149+2268+7558 2318.6,7632.4 12504 srgb(255,255,255)
I don't understand how any connected component of a black-and-white image can have an average value of "srgb(33,33,33)". I suspect there is a bug, perhaps that shows only in large images like this one.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-10T13:10:30-07:00
by Adam32
For testing I have now resized the page with punch holes on the right to 1241x1778 pixels (same size as left page). it still does not work. Here is the t2.png:
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-10T13:23:31-07:00
by snibgo
Well, try reducing the component size threshold.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-10T13:48:32-07:00
by Adam32
I reduced the component size threshold and that worked. Please see the final result, although it is not perfect and there are a few artifacts. Is there any way to solve this?
Also is there any way to get the script working with larger files? Or is it not a simple solution?
Many thanks for your help
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-11T05:29:55-07:00
by Adam32
This windows BAT works perfect for images with the punch holes on the left:
Code: Select all
convert 1.jpg -threshold 50%% -define connected-components:verbose=true -define connected-components:area-threshold=190 -define connected-components:mean-color=true -connected-components 8 t2.png
convert 1.jpg ( t2.png -crop 10%%x100%%+0+0 -morphology Erode disk:3 ) -alpha off -set option:compose:outside-overlay false -compose CopyOpacity -composite h.png
blurFill.bat h.png . h_bf.png
How do I use wildcards wit this, so the script works on all .jpg files? Or better still all .jpg, Tiff and PNG in the folder? I have had a go with this, but it does not work:
Code: Select all
convert *.jpg -threshold 50%% -define connected-components:verbose=true -define connected-components:area-threshold=190 -define connected-components:mean-color=true -connected-components 8 %i.png
convert *.jpg ( %i.png -crop 10%%x100%%+0+0 -morphology Erode disk:3 ) -alpha off -set option:compose:outside-overlay false -compose CopyOpacity -composite %i.png
blurFill.bat %i.png . %i.png
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-11T11:47:59-07:00
by snibgo
For very large images, you can resize them down to 1241x1778, and process that to create a mask (at the small size) that is black where the holes are. Resize that up to the full size, and copy the opacity to the large image. Then blur-fill it.
The large image shows the holes don't simply have a crescent shape at the bottom, but also marks at the top. When you have a mask that is black where the holes are, you can dilate the black marks with a kernel that operates just in the vertical drection, upwards.
For applying a script to multiple files, use a "for" loop.
Re: how to use ImageMagick to remove punch holes?
Posted: 2016-11-11T12:09:37-07:00
by Adam32
Thank you for your explanation for processing the large images
snibgo wrote:
For applying a script to multiple files, use a "for" loop.
Would you mind showing me how to do this with the code I have posted, as I have read up on for loops and can't seem to get this working.