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

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
Atze

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

Post 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
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

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

Post by rmagick »

Check out the thread about 6 below this one, "Make a box image for any image."
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Also see my bash script, squareup, at http://www.fmwconcepts.com/imagemagick/index.php
Atze

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

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

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

Post 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. :)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

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

Post 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');
?>
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

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

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

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

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

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