Page 1 of 2
vertical image alignment [solved]
Posted: 2017-04-13T17:17:51-07:00
by BradLee
Hi,
Is there anyway to vertically crop an image (of text) to the leftmost and rightmost colored (non-white) pixels?
The reason I'm asking is because I scanned an old out-of-print book to make an e-book, and the page margin keeps jumping between pages, and it's bothering me. Sometimes the text starts 40px in, sometimes 80px, sometimes 60-something.
So I'm hoping to crop the image to the leftmost and rightmost pixels and then re-padding it. If that makes any sense?
I'm using IM ImageMagick-7.0.5-4-Q16-x64-dll.exe In Windows 7.
Thanks.
Re: vertical image (page) alignment
Posted: 2017-04-13T17:55:03-07:00
by fmw42
What is the image format from the scanner? Perhaps it has a virtual canvas offset? This might be the case with PNG or TIFF.
If not, have you tried:
Code: Select all
convert image -fuzz XX% -trim +repage result
where XX% is the amount of tolerance to variations in the background color you may need if it is not completely uniform.
That will trim on all 4 sides.
See
http://www.imagemagick.org/Usage/crop/#trim
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/crop/#border
http://www.imagemagick.org/Usage/crop/#extent
If you just need it trimmed on two sides, then you can either
1) get the trim coordinates using -format "%@" info:, then parse and crop only in the dimension desired
or
2) Protect the trim once on one side and then again on the other side. See
http://www.imagemagick.org/Usage/crop/#trim_oneside
Note that sometimes scanning will cause dark spots in the scan due to unclean or dust on the scanner. This may not always be removed using -fuzz.
Re: vertical image (page) alignment
Posted: 2017-04-13T17:57:57-07:00
by fmw42
P.S. If the above does not help, please post an example image to some free hosting service such as dropbox.com and put the URL here so we can actually see what the issue might be and give you more exact commands.
Re: vertical image (page) alignment
Posted: 2017-04-13T18:11:22-07:00
by BradLee
Hi
fmw42,
The images are in PNG format.
There is no virtual canvas offset. It's just a matter of some sloppy (lazy) scanning.
I'm not too worried about dark spots, it doesn't need to be perfect. It's just the more dramatic shifts that I find off-putting.
Here's an example:
http://i1219.photobucket.com/albums/dd4 ... rxna3x.png
http://i1219.photobucket.com/albums/dd4 ... 8vdhb4.png
http://i1219.photobucket.com/albums/dd4 ... wejd7c.png
http://i1219.photobucket.com/albums/dd4 ... tpjfoo.png
It's not the most drastic one I have, but it still gives you an idea.
Re: vertical image (page) alignment
Posted: 2017-04-13T19:35:35-07:00
by fmw42
This should do it for your first image. You may need to change the fuzz value a little for different images. I have not padded it back. You can do that by adding -bordercolor white -border Bx0 just before the output, where B is the amount to pad on both the left and right sides. If using in a bat file, you will need to double the %
Code: Select all
magick 1000006441-I00000251-padded-cleaned_zpszrrxna3x.png ^
-background black -fuzz 8% ^
-gravity east -splice 1x0 ^
-trim +repage -gravity east -chop 1x0 ^
-gravity west -splice 1x0 ^
-trim +repage -gravity west -chop 1x0 ^
result.png
This is one way. There are likely other ways.
Re: vertical image (page) alignment
Posted: 2017-04-13T20:08:27-07:00
by fmw42
If you want the same width, but with the trimmed image placed back with a 20 pixel margin on the left, then you could do
Code: Select all
magick 1000006441-I00000251-padded-cleaned_zpszrrxna3x.png ^
-set option:dimensions "%wx%h" ^
-background black -fuzz 8% ^
-gravity east -splice 1x0 ^
-trim +repage -gravity east -chop 1x0 ^
-gravity west -splice 1x0 ^
-trim +repage -gravity west -chop 1x0 ^
-background white -gravity northwest -extent "%[dimensions]-20+0" ^
result.png
Re: vertical image (page) alignment
Posted: 2017-04-13T20:10:11-07:00
by BradLee
Perfect, thanks.
That's EXACTLY what I was looking for.
Re: vertical image (page) alignment
Posted: 2017-04-13T20:21:30-07:00
by BradLee
Is there a way to pad it evenly on both sides to the original page width?
Because I can't have an arbitrary amount of padding in case the images are different sizes.
Re: vertical image (page) alignment
Posted: 2017-04-13T20:29:13-07:00
by fmw42
try
Code: Select all
magick 1000006441-I00000251-padded-cleaned_zpszrrxna3x.png ^
-set option:dimensions "%wx%h" ^
-background black -fuzz 8% ^
-gravity east -splice 1x0 ^
-trim +repage -gravity east -chop 1x0 ^
-gravity west -splice 1x0 ^
-trim +repage -gravity west -chop 1x0 ^
-background white -gravity center -extent "%[dimensions]" ^
result.png
Re: vertical image (page) alignment
Posted: 2017-04-13T21:00:22-07:00
by BradLee
Yeah, that works.
Re: vertical image (page) alignment
Posted: 2017-04-14T00:20:16-07:00
by BradLee
I can't get it to output multiple files for some reason.
Can someone please take a look at my syntax and tell me what I'm doing wrong?
Code: Select all
magick *.png ^
-set option:dimensions "%wx%h" ^
-background black -fuzz 25% ^
-gravity east -splice 1x0 ^
-trim +repage -gravity east -chop 1x0 ^
-gravity west -splice 1x0 ^
-trim +repage -gravity west -chop 1x0 ^
-background white -gravity center -extent "%[dimensions]" ^
"c:\users\brad\downloads\cleaned\%[filename:f].png"
Thanks.
Re: vertical image (page) alignment
Posted: 2017-04-14T01:04:51-07:00
by snibgo
You use "filename:f" but you haven't set it anywhere.
Re: vertical image (page) alignment
Posted: 2017-04-14T02:56:40-07:00
by BradLee
snibgo wrote: ↑2017-04-14T01:04:51-07:00
You use "filename:f" but you haven't set it anywhere.
I tried setting it in the beginning, but then it throws me another error for option:dimensions
Code: Select all
magick *.png -set filename:f ^
-set option:dimensions "%wx%h" ^
-background black -fuzz 25% ^
-gravity east -splice 1x0 ^
-trim +repage -gravity east -chop 1x0 ^
-gravity west -splice 1x0 ^
-trim +repage -gravity west -chop 1x0 ^
-background white -gravity center -extent "%[dimensions]" ^
"c:\users\brad\downloads\cleaned\%[filename:f].png"
magick: unable to open image 'option:dimensions': No such file or directory @ er
ror/blob.c/OpenBlob/3094.
magick: no decode delegate for this image format `' @ error/constitute.c/ReadIma
ge/509.
Re: vertical image (page) alignment
Posted: 2017-04-14T03:01:27-07:00
by snibgo
You have to set it to something. Just like "option:dimensions" is set to something.
Re: vertical image (page) alignment
Posted: 2017-04-14T03:11:38-07:00
by BradLee
Ok, now I've got it.
Code: Select all
magick *.png -set filename:f "%[t]" ^
-set option:dimensions "%wx%h" ^
-background black -fuzz 25% ^
-gravity east -splice 1x0 ^
-trim +repage -gravity east -chop 1x0 ^
-gravity west -splice 1x0 ^
-trim +repage -gravity west -chop 1x0 ^
-background white -gravity center -extent "%[dimensions]" ^
"c:\users\ben\downloads\cleaned\%[filename:f].png"