Page 1 of 1
How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T02:07:39-07:00
by Bikeyan
I have a png with some transparency at board, and I want to trim the transparency, and keep every thing with same distance to the original center.
The original
The result I want.
The result with command
Code: Select all
convert original.png -trim +repage out.png
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T02:28:03-07:00
by snibgo
Please post the original PNG files with transparency, not screen-shots with checker backgrounds.
Please say what version of IM you use, on what platform.
I don't know what you mean by "keep every thing with same distance to the original center".
Perhaps you want to trim just the top and left? See
http://www.imagemagick.org/Usage/crop/#trim_oneside
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T03:06:55-07:00
by Bikeyan
The original file has a black dot in center, and some transparency at top, right, bottom, and left. I want to trim transparency as much as I can, while keep the original ratio aspect, and part's distance to center, after trim, the black dot should still in the center. But with command I post, after trim, the black dot move to right down.
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T03:20:31-07:00
by snibgo
To keep the original centre at the centre, you must trim the same amount from the left and right, and the same amount from the top and bottom.
To also keep the same aspect ratio (width/height), the amounts trimmed from the left-right edges and the top-bottom edges must be in the same ratio. Put this a different way: you must trim the same proportion from the left-right edges and the top-bottom edges.
In IM v6 this will need a script. In IM v7 it can probably be done in a single command.
Please post the original PNG files with transparency, not screen-shots with checker backgrounds.
Please say what version of IM you use, on what platform.
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T05:19:11-07:00
by Bikeyan
My IM version in window7 is V7.0.7-31, in ubuntu is V6.7.7-10, so, both version I have. But I prefer with linux shell script than dos, if V7 is good at do this staff, maybe I should update the ubuntu IM Version.
Thank you for your attention.
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T06:02:55-07:00
by snibgo
Your image is square. To fulfil your conditions, you need to shave the same amount from each side. This will be the minimum of the four trims that "-trim" gives.
A single command for V7. This is Windows CMD syntax; bash needs parentheses to be escaped.
magick lBW9I.png ( +clone -trim -set option:NTRIM %[fx:min(min(min(page.x,page.y),page.width-w-page.x),page.height-h-page.y)] +delete ) -shave %[NTRIM] x.png
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T09:51:31-07:00
by fmw42
If on Unix like system, you can try my script centertrim at my link below.
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-08T15:27:51-07:00
by GeeMack
Bikeyan wrote: ↑2018-05-08T05:19:11-07:00My IM version in window7 is V7.0.7-31, in ubuntu is V6.7.7-10, so, both version I have. But I prefer with linux shell script than dos, if V7 is good at do this staff, maybe I should update the ubuntu IM Version.
I can get your described result by using IM 6.8.9-9 in the ubuntu bash shell in Windows 10, running this command...
Code: Select all
convert test.png -write mpr:input -duplicate 3 \
-distort SRT %[fx:t*90] -background none -layers merge -trim \
-set option:pagewide %[w] -set option:pagehigh %[h] -set option:pageoffset %[fx:page.x] \
-delete 0 mpr:input -set page %[pagewide]x%[pagehigh]-%[pageoffset]-%[pageoffset] \
-coalesce result.png
That reads in the input, stores one copy in temporary memory, makes 3 more copies of it, and rotates them 0, 90, 180, and 270 degrees. Then it flattens them and uses the trim results from that to calculate the final image dimensions and offsets. Next it deletes that modified image, brings the original back from temporary memory, and creates the output by applying those calculated page settings to the original.
This should work with square images. If the width and height dimensions are not the same, the calculations would become more complicated.
EDITED TO ADD: The command below should take any input image and trim away as much transparent as possible while keeping the original center pixel(s) at the center of the result...
Code: Select all
convert test.png -write mpr:input -background none \
-rotate 180 mpr:input -composite -set page %[@] \
-set option:bounds %[fx:u.page.width]x%[fx:u.page.height]-%[fx:u.page.x]-%[fx:u.page.y] \
-delete 0 mpr:input -set page %[bounds] -coalesce result.png
It trims the same amount from the top and the bottom, and trims the same amount from the left and right, but the top/bottom amount may be different from the left/right amount.
I tested it in bash shell and in Windows command line with IM6 and IM7 with only minor tweaks to the syntax.
There may be other simpler ways to do it with IM7.
Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?
Posted: 2018-05-09T04:58:24-07:00
by Bikeyan
Thanks to all guys, all your commands works! Because some of my pngs are not square, so I modify snibgo's command a little.
Code: Select all
magick lBW9I.png ( +clone -trim -set option:NTRIM %[fx:min(min(page.x,page.width-w-page.x),min(page.y,page.height-h-page.y)*page.width/page.height)]x%[fx:min(min(page.y,page.height-h-page.y),min(page.x,page.width-w-page.x)*page.height/page.width)] +delete ) -shave %[NTRIM] x.png