vertical image alignment [solved]
vertical image alignment [solved]
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.
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.
Last edited by BradLee on 2017-04-14T23:20:17-07:00, edited 2 times in total.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: vertical image (page) alignment
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:
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.
If not, have you tried:
Code: Select all
convert image -fuzz XX% -trim +repage result
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: vertical image (page) alignment
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
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: vertical image (page) alignment
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 %
This is one way. There are likely other ways.
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: vertical image (page) alignment
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
Perfect, thanks.
That's EXACTLY what I was looking for.
That's EXACTLY what I was looking for.
Re: vertical image (page) alignment
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.
Because I can't have an arbitrary amount of padding in case the images are different sizes.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: vertical image (page) alignment
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
Yeah, that works.
Re: vertical image (page) alignment
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?
Thanks.
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"
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: vertical image (page) alignment
You use "filename:f" but you haven't set it anywhere.
snibgo's IM pages: im.snibgo.com
Re: vertical image (page) alignment
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: vertical image (page) alignment
You have to set it to something. Just like "option:dimensions" is set to something.
snibgo's IM pages: im.snibgo.com
Re: vertical image (page) alignment
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"