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):
I hope you understand me and my needs.
Thanks in advance,
Atze
How to commandline (convert.exe) to trim and square a jpg?
Re: How to commandline (convert.exe) to trim and square a jpg?
Check out the thread about 6 below this one, "Make a box image for any image."
- 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?
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?
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:
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:
Maybe you have another hint for me to combine the trimming, squaring and using of the trimmed maximum extends in one commandline?
Regards,
Atze
@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:
The problem is, that the input.jpg will not be trimmed before it will be squared.convert.exe c:\input.jpg -resize "100x100^" -gravity center -crop 100x100+0+0 +repage c:\output.jpg
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?
It looks like I got it nearly meanwhile:
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:
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.
Info:convert.exe c:\input.jpg -trim -thumbnail "152x152>" -gravity center -extent 152x152 c:\output.jpg
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:
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).convert.exe c:\input.jpg -gravity center -trim -extent "152x152" c:\output.jpg
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?
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.
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');
?>
- 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?
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
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?
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
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
- 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?
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?
Thank you very much again, guys!
Finally I got it converted to DOS-Syntax and it works well:
Atze
Finally I got it converted to DOS-Syntax and it works well:
Or all in one row: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
Great forum here!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
Atze