Page 1 of 2
Trim image and return trimmed amount
Posted: 2016-10-28T19:00:52-07:00
by chaoscarnage
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?
Re: Trim image and return trimmed amount
Posted: 2016-10-28T19:31:48-07:00
by fmw42
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
Re: Trim image and return trimmed amount
Posted: 2016-10-28T23:08:39-07:00
by GeeMack
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.
Re: Trim image and return trimmed amount
Posted: 2016-10-29T08:23:01-07:00
by chaoscarnage
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?
Re: Trim image and return trimmed amount
Posted: 2016-10-29T08:29:23-07:00
by snibgo
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".
Re: Trim image and return trimmed amount
Posted: 2016-10-29T10:47:15-07:00
by chaoscarnage
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?
Re: Trim image and return trimmed amount
Posted: 2016-10-29T10:53:29-07:00
by fmw42
Sure you can have the output have the same name as the input. But it will overwrite your input. So make backups.
Re: Trim image and return trimmed amount
Posted: 2016-10-29T10:57:21-07:00
by chaoscarnage
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');
Re: Trim image and return trimmed amount
Posted: 2016-10-29T11:20:47-07:00
by snibgo
What error messages do you get? Your "exec" isn't capturing error messages. Why not?
Re: Trim image and return trimmed amount
Posted: 2016-10-29T11:25:47-07:00
by chaoscarnage
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.
Re: Trim image and return trimmed amount
Posted: 2016-10-29T11:43:51-07:00
by snibgo
"-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?
Re: Trim image and return trimmed amount
Posted: 2016-10-29T17:35:27-07:00
by chaoscarnage
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]"
Re: Trim image and return trimmed amount
Posted: 2016-10-29T18:07:37-07:00
by chaoscarnage
I believe I have found the problem with all the calculations I was doing. Thank you all very much for your support.
Re: Trim image and return trimmed amount
Posted: 2016-10-29T18:25:40-07:00
by GeeMack
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.
Re: Trim image and return trimmed amount
Posted: 2016-10-29T19:12:20-07:00
by fmw42
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.