Trim image and return trimmed amount
-
- Posts: 93
- Joined: 2012-12-31T15:56:29-07:00
- Authentication code: 6789
Trim image and return trimmed amount
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?
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?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Trim image and return trimmed amount
Code: Select all
convert image -format "%@" info:
Code: Select all
convert image -format "%wx%h" info:
See http://www.imagemagick.org/script/escape.php
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Trim image and return trimmed amount
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...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?
Code: Select all
convert image.png -format "%w %h %@" info:
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:
-
- Posts: 93
- Joined: 2012-12-31T15:56:29-07:00
- Authentication code: 6789
Re: Trim image and return trimmed amount
Fantastic, this is what I am using to get the vars,
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?
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:-'));
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Trim image and return trimmed amount
For that, use "-crop".chaoscarnage wrote:How would I apply a trim where I hand it x to img, x from img, y to, y from?
snibgo's IM pages: im.snibgo.com
-
- Posts: 93
- Joined: 2012-12-31T15:56:29-07:00
- Authentication code: 6789
Re: Trim image and return trimmed amount
I have crop working, I cannot save it out as the same image name though. Is this possible?snibgo wrote:For that, use "-crop".chaoscarnage wrote:How would I apply a trim where I hand it x to img, x from img, y to, y from?
Code: Select all
shell_exec('convert ' . $path . 'df845d8bf95dabb50c71da3255.png -crop ' . $test[0] . 'x' . $test[1] . '+' . $test[2] . '+'.$test[3] . ' ' . $path . 'test3.png');
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Trim image and return trimmed amount
Sure you can have the output have the same name as the input. But it will overwrite your input. So make backups.
-
- Posts: 93
- Joined: 2012-12-31T15:56:29-07:00
- Authentication code: 6789
Re: Trim image and return trimmed amount
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?fmw42 wrote:Sure you can have the output have the same name as the input. But it will overwrite your input. So make backups.
Code: Select all
exec('convert ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png -crop ' . $test[0] . 'x' . $test[1] . '+' . $test[2] . '+'.$test[3] . ' -composite ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png');
Code: Select all
exec('convert ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png -crop ' . $test[0] . 'x' . $test[1] . '+' . $test[2] . '+'.$test[3] . ' ' . $path['temp'] . 'df845d8bf95dabb50c71da3255.png');
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Trim image and return trimmed amount
What error messages do you get? Your "exec" isn't capturing error messages. Why not?
snibgo's IM pages: im.snibgo.com
-
- Posts: 93
- Joined: 2012-12-31T15:56:29-07:00
- Authentication code: 6789
Re: Trim image and return trimmed amount
I had shell_exec, there was no message at all.snibgo wrote:What error messages do you get? Your "exec" isn't capturing error messages. Why not?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Trim image and return trimmed amount
"-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
-
- Posts: 93
- Joined: 2012-12-31T15:56:29-07:00
- Authentication code: 6789
Re: Trim image and return trimmed amount
Okay, I have everything working, but now it is not quite turning out right.
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]"
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');
$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]"
-
- Posts: 93
- Joined: 2012-12-31T15:56:29-07:00
- Authentication code: 6789
Re: Trim image and return trimmed amount
I believe I have found the problem with all the calculations I was doing. Thank you all very much for your support.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Trim image and return trimmed amount
You can simplify the way you're assembling the crop specs with something like this...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');
Code: Select all
convert image.png -format "%@" info:
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Trim image and return trimmed amount
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.