Command line image converter - Generate thumbs to size, with filler!

Soldato
Joined
15 Nov 2003
Posts
14,473
Location
Marlow
OK, I need a command line image converter that convert any image (JPG) to an exact size, eg: 400x300. The trick is, if the image, due to it's aspect ration, does not fit in 400x300, then place it in there with black borders.

eg: A 900x1200 image would be converted to 225x300 to retain its aspect ratio, and then given black borders left and right to make it 400x300.

Make sense?

A 1200x600 image, would be converted to 400x200, and given black borders top and bottom to make it 400x300.
 
ImageMagick will do it with a bit of prodding, and it has win32 and *nix binaries.

I've not got any binaries on this PC so I can't test, but I'm fairly sure this will work:

Code:
convert big.jpg -resize 400x300 -background black -mavity center -extent 400x300 small.jpg

(Resize to maximum dimensions of 400x300, maintaining aspect ratio; set the background to black; put the image in the center; extend the "canvas" to 400x300. Replace big.jpg and small.jpg with the original file and the desired output file.)
 
ImageMagick will do it with a bit of prodding, and it has win32 and *nix binaries.

I've not got any binaries on this PC so I can't test, but I'm fairly sure this will work:

Code:
convert big.jpg -resize 400x300 -background black -mavity center -extent 400x300 small.jpg

(Resize to maximum dimensions of 400x300, maintaining aspect ratio; set the background to black; put the image in the center; extend the "canvas" to 400x300. Replace big.jpg and small.jpg with the original file and the desired output file.)
Fab! I was looknig at Irfanview. I think it can do it, but not sure how from a command line...
 
Sorry... "installed some binaries"?

I mean I installed ImageMagick on this machine to test—I didn't have it before, I was just doing it from memory from the last time I used it. The line I posted above does work, and produces images like this:

xf97v5.jpg


30911tw.jpg


(Both 400x300.)
 
I mean I installed ImageMagick on this machine to test—I didn't have it before, I was just doing it from memory from the last time I used it. The line I posted above does work, and produces images like this:

xf97v5.jpg


30911tw.jpg


(Both 400x300.)

Beautiful! Now as long as you can do that from a command line, great!

Will have a play!
 
You can? Just run the command I posted!

Not sure if I follow? Your example was:-

convert big.jpg -resize 400x300 -background black -mavity center -extent 400x300 small.jpg

I'm talking about a dos command line? Your example sort of looks like one, except what's "convert"? :)
 
Not sure if I follow? Your example was:-

convert big.jpg -resize 400x300 -background black -mavity center -extent 400x300 small.jpg

I'm talking about a dos command line? Your example sort of looks like one, except what's "convert"? :)

Download ImageMagick from the ImageMagick website, install it, then type that into your DOS command line. That's the whole point of ImageMagick: it gives you a way to do these sorts of things from the command line.
 
Download ImageMagick from the ImageMagick website, install it, then type that into your DOS command line. That's the whole point of ImageMagick: it gives you a way to do these sorts of things from the command line.

Oh! OK! I'll have a play :)

Obviously an app called "convert" then... Seems a rather vague/open executable name though :confused:
 
ImageMagick will do it with a bit of prodding, and it has win32 and *nix binaries.

I've not got any binaries on this PC so I can't test, but I'm fairly sure this will work:

Code:
convert big.jpg -resize 400x300 -background black -mavity center -extent 400x300 small.jpg

(Resize to maximum dimensions of 400x300, maintaining aspect ratio; set the background to black; put the image in the center; extend the "canvas" to 400x300. Replace big.jpg and small.jpg with the original file and the desired output file.)

OK, I've been playing around with this...

What I've notice while generating thumbnails is give the following two examples in VB:-
ImageMagick.Convert(public_txtSourcePath + FileName, "-resize", "400x300", "-quality", "50", public_txtThumbPath + FileName)

ImageMagick.Convert(public_txtSourcePath + FileName, "-thumbnail", "400x300", "-quality", "50", public_txtThumbPath + FileName)​
In one particular case the -resize gives an image of 72KB, where as the -thumb gives a far image size of 8K, with the images looking the same.



So obviously the -thumb is ideal! However, the -thumb doesn't seem to like the -extent coding.

So this works:-
ImageMagick.Convert(public_txtSourcePath + FileName, "-resize", "400x300", "-background", "black", "-mavity", "center", "-extent", "400x300", "-quality", "50", public_txtThumbPath + FileName)
But this does not:-
ImageMagick.Convert(public_txtSourcePath + FileName, "-thumbnail", "400x300", "-background", "black", "-mavity", "center", "-extent", "400x300", "-quality", "50", public_txtThumbPath + FileName)​


Have you got any idea how either:-
1) To get the -extent coing to work with the -thumbnail request?
2) How to get a -resize to produce images as small/efficient as the -thumb?

Thanks!



EDIT: -strip does the trick with the -resize option. It makes the difference between a 72K image, or an 8K image!!
 
Last edited:
Back
Top Bottom