How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

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?".
Post Reply
Bikeyan
Posts: 4
Joined: 2018-05-08T02:01:48-07:00
Authentication code: 1152

How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

Post 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
Image
The result I want.
Image
The result with command

Code: Select all

convert original.png -trim +repage out.png
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

Post 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
snibgo's IM pages: im.snibgo.com
Bikeyan
Posts: 4
Joined: 2018-05-08T02:01:48-07:00
Authentication code: 1152

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

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

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

Post 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.
snibgo's IM pages: im.snibgo.com
Bikeyan
Posts: 4
Joined: 2018-05-08T02:01:48-07:00
Authentication code: 1152

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

Post by Bikeyan »

Image

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

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

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

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

Post by fmw42 »

If on Unix like system, you can try my script centertrim at my link below.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

Post 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.
Bikeyan
Posts: 4
Joined: 2018-05-08T02:01:48-07:00
Authentication code: 1152

Re: How to trim a png with ImageMigick convert tool, and keep all things same distance to original center?

Post 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
Post Reply