Page 1 of 1

How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-18T09:59:49-07:00
by Atze
Hi.

I am quite helpless after digging in the online-references, so I try to ask the experts here.

What I already have is a windows-commandline for "convert.exe" which trims my JPGs.
But what I need additionally is to square the picture without destroying the aspect-ratio.
The size of the square has to be the height or the width of the trimmed picture, depending on which dimension is bigger.
I already tried and tried using extending and padding syntax, but I did not get it.

I added some samples for you (original, trimmed, squared):

Image Image Image

Image Image Image

I hope you understand me and my needs.

Thanks in advance,
Atze

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-18T15:48:40-07:00
by rmagick
Check out the thread about 6 below this one, "Make a box image for any image."

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-18T17:03:20-07:00
by fmw42
Also see my bash script, squareup, at http://www.fmwconcepts.com/imagemagick/index.php

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-19T01:06:38-07:00
by Atze
Thanks for your replies.

@fmw42:
I already dig in your script "squareup", but I did not get rid of it. Unluckily. :-(

@rmagik:
I took a look in the thread you told me, and I tried again, to fulfill it on my own, but still I got not the result I need.
I tried the following commandline now for testing:
convert.exe c:\input.jpg -resize "100x100^" -gravity center -crop 100x100+0+0 +repage c:\output.jpg
The problem is, that the input.jpg will not be trimmed before it will be squared.
If I add "-trim", the result is gone totally, means I get an empty image (looks like).
Additionally a backdraw of this commandline I used in the example is, that I would have to provide the size (100x100) fixed.
But it should be taken from the trimmed "input.jpg".

Additional info:
When I use this commandline only, the trimming is well done:
convert.exe c:\input.jpg -trim +repage c:\output.jpg

Maybe you have another hint for me to combine the trimming, squaring and using of the trimmed maximum extends in one commandline?

Regards,
Atze

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-19T01:59:37-07:00
by Atze
It looks like I got it nearly meanwhile:
convert.exe c:\input.jpg -trim -thumbnail "152x152>" -gravity center -extent 152x152 c:\output.jpg
Info:
152 is the maximum height/width of the trimmed input.jpg (I trimmed it first and took a look on the resulting image-properties).

So my 'only' problem now is to retrieve and use the maximum height/width of the result of the "-trim" command to use it in the "-thumbnail" and "-extend" commands.

Is there a way to do this in the commandline?


Edit:
Meanwhile I ended up in this commandline:
convert.exe c:\input.jpg -gravity center -trim -extent "152x152" c:\output.jpg
When I try to use the dynamical results of the trim-command by using "%hx%h" in the extend-command, it seems not to regognize them (also usage of double %% does not change it's behaviour).
Furthermore I surely would need to know, which one is bigger (%w or %h).

It's really an difficult approach.

Still need some help, please. :)

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-19T11:09:30-07:00
by Bonzo
One way to do this is install a server on your PC such as XAMPP and use the comand line through php.

Something like below - the commented out lines use composite rather than extent and also work.

Code: Select all

<?php
// Trim the image
exec("convert verticalki3.jpg -trim temp.png");
// Get the trimmed image dimensions
$size = getimagesize('temp.png');
// Set the width variable
$width = $size[0];
// Set the height variable
$height = $size[1];
// If its wider than higher do this conversion
if ( $width > $height ){
//exec("convert -size {$width}x{$width} xc:white temp.png -gravity center -composite output.jpg");
exec("convert temp.png -gravity center -extent {$width}x{$width} output.jpg");
}
// If its heigher than wider do this conversion
else {
//exec("convert -size {$height}x{$height} xc:white temp.png -gravity center -composite output.jpg");
exec("convert temp.png -gravity center -extent {$height}x{$height} output.jpg");
}
// Delete the tempory image
unlink('temp.png');
?>

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-19T12:29:41-07:00
by fmw42
It takes two lines. First get maximum dimension after trimming. Then trim and extend to that size.


dim=`convert horizontalhn1.jpg -trim -format "%[fx:max(h,w)]" info:`
convert horizontalhn1.jpg -trim -background white -gravity center -extent ${dim}x${dim} horizontalhn1_square.jpg

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-19T13:06:06-07:00
by Atze
Thank you very much.
What kind of script is this?
I guess no Windows-DOS batch, or?

I think I can rewrite it to DOS-Syntax.
But this means, I can't call it from one commandline, right?
I will take a look if I can combine it in the Batch to one line.

Regards,
Atze

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-19T15:20:48-07:00
by fmw42
Atze wrote:Thank you very much.
What kind of script is this?
I guess no Windows-DOS batch, or?

I think I can rewrite it to DOS-Syntax.
But this means, I can't call it from one commandline, right?
I will take a look if I can combine it in the Batch to one line.

Regards,
Atze

unix shell script, but each of the two lines are just IM code. The first line stores a variable for the max size that is used then in the second line.

see http://www.imagemagick.org/Usage/api/#windows for windows scripting issues

see http://www.imagemagick.org/Usage/transform/#fx_escapes for IM -fx calculations

Re: How to commandline (convert.exe) to trim and square a jpg?

Posted: 2009-01-20T00:50:08-07:00
by Atze
Thank you very much again, guys! :D

Finally I got it converted to DOS-Syntax and it works well:
FOR /F "delims=" %%A IN ('convert.exe input.jpg -trim -format "%%[fx:max(h,w)]" info:') DO SET DIM=%%A
convert.exe input.jpg -trim -background white -gravity center -extent %DIM%x%DIM% output.jpg
Or all in one row:
FOR /F "delims=" %%A IN ('convert.exe input.jpg -trim -format "%%[fx:max(h,w)]" info:') DO convert.exe input.jpg -trim -background white -gravity center -extent %%Ax%%A output.jpg
Great forum here!

Atze