vertical image alignment [solved]

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

vertical image alignment [solved]

Post 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.
Last edited by BradLee on 2017-04-14T23:20:17-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: vertical image (page) alignment

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: vertical image (page) alignment

Post 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.
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image (page) alignment

Post 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. :oops:

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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: vertical image (page) alignment

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: vertical image (page) alignment

Post 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
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image (page) alignment

Post by BradLee »

Perfect, thanks.
That's EXACTLY what I was looking for. :)
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image (page) alignment

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: vertical image (page) alignment

Post 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
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image (page) alignment

Post by BradLee »

Yeah, that works.
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image (page) alignment

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: vertical image (page) alignment

Post by snibgo »

You use "filename:f" but you haven't set it anywhere.
snibgo's IM pages: im.snibgo.com
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image (page) alignment

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: vertical image (page) alignment

Post by snibgo »

You have to set it to something. Just like "option:dimensions" is set to something.
snibgo's IM pages: im.snibgo.com
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image (page) alignment

Post 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"
:)
Post Reply