Trim image and return trimmed amount

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?".
chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Trim image and return trimmed amount

Post by chaoscarnage » 2016-10-28T19:00:52-07:00

Even if the trim must come later, is there anyway to find out how much of an image has been trimmed on each side? I need it to compare to images, so they can be trimmed the same amount and resized.

My thought right now is to trim the images, then compare the sizes, but that will not tell much how much was taken off the left and right sides or top and bottom.

Is this at all possible?

I can use 'convert df845d8bf95dabb50c71da3255.png -trim info:-' I am working in php so I could do shell_exec but how do I get the information into a usable format?

User avatar
fmw42
Posts: 25656
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim image and return trimmed amount

Post by fmw42 » 2016-10-28T19:31:48-07:00

Code: Select all

convert image -format "%@" info:
will give you the trim size and the offset of the top left corner, from that you can compare to the input size and compute how much from each side was removed. You will have to save the result into a PHP variable and then use PHP to parse the W,H,X,Y. Then get the image size

Code: Select all

convert image -format "%wx%h" info:
And get that W,H and compute how much was removed from each side using the previous W,H,X,Y.

See http://www.imagemagick.org/script/escape.php

User avatar
GeeMack
Posts: 722
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Trim image and return trimmed amount

Post by GeeMack » 2016-10-28T23:08:39-07:00

chaoscarnage wrote:My thought right now is to trim the images, then compare the sizes, but that will not tell much how much was taken off the left and right sides or top and bottom. [...] I can use 'convert df845d8bf95dabb50c71da3255.png -trim info:-' I am working in php so I could do shell_exec but how do I get the information into a usable format?
I don't know PHP, but I'd assume there's a way to parse a simple string into a few variables. Going from the advice fmw42 gave above, this command will retrieve the current image's width and height, and the dimensions after the "-trim", and the coordinates from the left and top where the trim begins...

Code: Select all

convert image.png -format "%w %h %@" info:
The output of that will be a text string looking like "720 480 637x392+17+33".

You can let IM do some of the calculations using a few of the built-in FX expressions to output more specific information. For example...

Code: Select all

convert image.png +repage -trim \
   -format '%[fx:page.width] %[fx:page.x] %[fx:page.width - page.x - w] %[fx:w]' info:
That command would ouput something like "720 60 20 640", which is the original width of the image, followed by the amount trimmed from the left, then the amount trimmed from the right, and last, the width after trimming. Details about the height can be obtained just as easily and added to that same command. You can find out more at this LINK.

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Trim image and return trimmed amount

Post by chaoscarnage » 2016-10-29T08:23:01-07:00

Fantastic, this is what I am using to get the vars,

Code: Select all

shell_exec('convert ' . $build_img . '.png -format "%[fx:page.x] %[fx:page.width - page.x - w] %[fx:page.y] %[fx:page.height - page.y - h]" -trim info:-'));
then I foreach them and have the lowest possible vars.

Now, all I need to be able to do is apply a specific trim to the image? How would I apply a trim where I hand it x to img, x from img, y to, y from?

snibgo
Posts: 12272
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim image and return trimmed amount

Post by snibgo » 2016-10-29T08:29:23-07:00

chaoscarnage wrote:How would I apply a trim where I hand it x to img, x from img, y to, y from?
For that, use "-crop".
snibgo's IM pages: im.snibgo.com

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Trim image and return trimmed amount

Post by chaoscarnage » 2016-10-29T10:47:15-07:00

snibgo wrote:
chaoscarnage wrote:How would I apply a trim where I hand it x to img, x from img, y to, y from?
For that, use "-crop".
I have crop working, I cannot save it out as the same image name though. Is this possible?

Code: Select all

shell_exec('convert ' . $path . 'df845d8bf95dabb50c71da3255.png -crop ' . $test[0] . 'x' . $test[1] . '+' . $test[2] . '+'.$test[3] . ' ' . $path . 'test3.png');
If I instead replace test3 with the original name, it does not save. Any thoughts?

User avatar
fmw42
Posts: 25656
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim image and return trimmed amount

Post by fmw42 » 2016-10-29T10:53:29-07:00

Sure you can have the output have the same name as the input. But it will overwrite your input. So make backups.

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Trim image and return trimmed amount

Post by chaoscarnage » 2016-10-29T10:57:21-07:00

fmw42 wrote:Sure you can have the output have the same name as the input. But it will overwrite your input. So make backups.
It's not working for me. I have tried both with and without composite and the image simply does not get replaced. Is there something I am doing wrong?

Code: Select all

exec('convert ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png -crop ' . $test[0] . 'x' . $test[1] . '+' . $test[2] . '+'.$test[3] . ' -composite ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png');
and

Code: Select all

exec('convert ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png -crop ' . $test[0] . 'x' . $test[1] . '+' . $test[2] . '+'.$test[3] . '  ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png');

snibgo
Posts: 12272
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim image and return trimmed amount

Post by snibgo » 2016-10-29T11:20:47-07:00

What error messages do you get? Your "exec" isn't capturing error messages. Why not?
snibgo's IM pages: im.snibgo.com

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Trim image and return trimmed amount

Post by chaoscarnage » 2016-10-29T11:25:47-07:00

snibgo wrote:What error messages do you get? Your "exec" isn't capturing error messages. Why not?
I had shell_exec, there was no message at all.

snibgo
Posts: 12272
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim image and return trimmed amount

Post by snibgo » 2016-10-29T11:43:51-07:00

"-composite" is wrong because that needs 2 or 3 input images but you have only one. The other command looks okay but what are the values of the variables $test[0] etc? And what is the size of the input image? Does it already have offsets?
snibgo's IM pages: im.snibgo.com

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Trim image and return trimmed amount

Post by chaoscarnage » 2016-10-29T17:35:27-07:00

Okay, I have everything working, but now it is not quite turning out right.

Code: Select all

exec('mogrify -crop ' . $return_var[0] . 'x' . $return_var[1] . '+' . $return_var[2] . '+'. $return_var[3] . ' +repage -resize ' . ICON_SIZE . 'x' . ICON_SIZE . ' ' . $row[0] . '.png');
I need it to crop the image and then resize it down to the specified size, but the images it is returning are all kinds of messed up. Does something look wrong with this?

$return_var[0] = "%[fx:page.x]"
$return_var[1] = "%[fx:page.width - page.x - w]"
$return_var[2] = "%[fx:page.y]"
$return_var[3] = " %[fx:page.height - page.y - h]"

chaoscarnage
Posts: 93
Joined: 2012-12-31T15:56:29-07:00
Authentication code: 6789

Re: Trim image and return trimmed amount

Post by chaoscarnage » 2016-10-29T18:07:37-07:00

I believe I have found the problem with all the calculations I was doing. Thank you all very much for your support.

User avatar
GeeMack
Posts: 722
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Trim image and return trimmed amount

Post by GeeMack » 2016-10-29T18:25:40-07:00

chaoscarnage wrote:Okay, I have everything working, but now it is not quite turning out right.

Code: Select all

exec('mogrify -crop ' . $return_var[0] . 'x' . $return_var[1] . '+' . $return_var[2] . '+'. $return_var[3] . ' +repage -resize ' . ICON_SIZE . 'x' . ICON_SIZE . ' ' . $row[0] . '.png');
You can simplify the way you're assembling the crop specs with something like this...

Code: Select all

convert image.png -format "%@" info:
The output of that command will be in the form of "843x622+38+46". It's the size in pixels of the image remaining after a "-trim" and the upper left coordinates where the trim starts in the original, like WxH+X+Y. If you put that output directly into a variable and use it to specify the "-crop" size and starting coordinates, the results would be the same as trimming it. That format escape "%@" returns exactly that.

User avatar
fmw42
Posts: 25656
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim image and return trimmed amount

Post by fmw42 » 2016-10-29T19:12:20-07:00

Perhaps I miss something. But what is the point of computing the trim coordinates and using them to crop without any other changes. You might as well just use -trim.

Post Reply