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

Re: vertical image (page) alignment

Post by BradLee »

Now I'm looking to so something similar, but horizontally instead.

I don't want to center the cropped images horizontally, because all the pages are different lengths
So I want to add a 28px border to the top instead
Then I want to pad the remainder of the image at the bottom to make it 1024px high.

I've managed to create the following code so far to crop the top and the bottom of the image...

Code: Select all

magick *.png -set filename:f "%[t]" ^
-set option:dimensions "%wx%h" ^
-background black -fuzz 25% ^
-gravity north -splice 0x1 ^
-trim +repage -gravity north -chop 0x1 ^
-gravity south -splice 0x1 ^
-trim +repage -gravity south -chop 0x1 ^
"c:\users\brad\downloads\cleaned\%[filename:f].png"
But I am coming unstuck trying to add a padded border to the top of the image?
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: vertical image (page) alignment

Post by GeeMack »

BradLee wrote: 2017-04-14T07:33:33-07:00But I am coming unstuck trying to add a padded border to the top of the image?
Try adding something like this just before the output of your command...

Code: Select all

... -background white -gravity north -splice 0x28 -extent x1024 ...
That sets the background color to white, sets the gravity to north, splices 28 pixels of the background color onto the top, then keeps all that to the north while making the output a total of 1024 pixels high. For the "-extent" operation you can use that "%[dimensions]" variable you set instead of "x1024" to make the output match the original dimensions.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: vertical image alignment (updated question)

Post by fmw42 »

If you want to trim the image, then pad 28 at the top and make the result 1024 tall with the trimmed image in the middle horizontally of your original width, then you can do

Code: Select all

magick 1000006441-I00000251-padded-cleaned_zpszrrxna3x.png ^
-set option:wid "%w" ^
-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 north -extent "%[wid]x1024+0-28" ^
result.png
This assumes that your trim will make the image smaller vertically than 1024-28=996.

If you want something else, please clarify about the width.
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image alignment (updated question)

Post by BradLee »

GeeMack has the right idea. ;)

I want to trim the top, pad 28px at the top, then pad the remaining image to 1024px height.
Last edited by BradLee on 2017-04-14T19:24:31-07:00, edited 1 time 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 alignment (updated question)

Post by fmw42 »

Try this. It stores the original image width. Then it does a trim only at the top, by padding the bottom with one row of black. Then I chop off the bottom black row. Then I extend the image so that it is centered horizontally but shifted down 28 pixels and filled with white to 1024 height.

Code: Select all

magick 1000006441-I00000251-padded-cleaned_zpszrrxna3x.png ^
-set option:wid "%w" ^
-background black -fuzz 10% ^
-gravity south -splice 0x1 ^
-trim +repage -gravity south -chop 0x1 ^
-background white -gravity north -extent "%[wid]x1024+0-28" ^
result.png
BradLee
Posts: 23
Joined: 2017-04-11T05:09:01-07:00
Authentication code: 1151

Re: vertical image alignment (updated question)

Post by BradLee »

Thanks fmw42, that worked.
I added the trimming at the bottom however, just to make sure that there were no extraneous not-quite-white pixels.
Cheers. ;)

Thanks GeeMack too.

Have a good Easter guys, whether you celebrate it or not!
Post Reply