Any experts in using ImageMagick lurking?
I need to resize an image to fit into a 350x350 box. Which means I need to (a) scale the image so no dimension exceeds 350, then (b) centre the image in a 350x350 canvas.
Eg for a 700x600 image, (a) creates a 350x300 image which (b) then converts to 350x350 without distorting it (by adding "whitespace".
Ideally I want some control over what the canvas colour ("whitespace") will be, in particular transparent would be a good option, but white will do for now.
The first bit is easy: convert -resize 350x350 old.png new.png I've tried various ways to do step (b) without success. (A single command which does both would be best, but two separate commands would be a start!)
On 18-Mar-09 12:12:03, Mark Rogers wrote:
Any experts in using ImageMagick lurking?
I need to resize an image to fit into a 350x350 box. Which means I need to (a) scale the image so no dimension exceeds 350, then (b) centre the image in a 350x350 canvas.
Eg for a 700x600 image, (a) creates a 350x300 image which (b) then converts to 350x350 without distorting it (by adding "whitespace".
Ideally I want some control over what the canvas colour ("whitespace") will be, in particular transparent would be a good option, but white will do for now.
The first bit is easy: convert -resize 350x350 old.png new.png I've tried various ways to do step (b) without success. (A single command which does both would be best, but two separate commands would be a start!)
-- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0845 45 89 555 Registered in England (0456 0902) at 13 Clarke Rd, Milton Keynes, MK1 1LG
Have a look at 'montage' (part of the InageMagick package). While it is usually used for making a true montage out of several separate images, there is nothing to stop you using it for just one.
The options will allow you to do things like placement, borders, etc.
montage -help
lists the options.
Hpoing this helps! Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) Ted.Harding@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 18-Mar-09 Time: 12:31:10 ------------------------------ XFMail ------------------------------
(Ted Harding) wrote:
Have a look at 'montage' (part of the InageMagick package). While it is usually used for making a true montage out of several separate images, there is nothing to stop you using it for just one.
Thanks, because this lead me to the option I needed in convert!
What I need is really simple: convert -resize 350x350 -extent 350x350 -gravity center old.png new.png
On 18/3/09 12:12, Mark Rogers wrote:
I need to resize an image to fit into a 350x350 box. Which means I need to (a) scale the image so no dimension exceeds 350, then (b) centre the image in a 350x350 canvas.
If you're prepared to use NetPBM, you should be able to use pnmpaste or pnmpad to do this. It's a 10-minute job to roll out a program to do this, so give me a shout if you can't find anything that works.
..Adrian
Mark Rogers wrote:
Any experts in using ImageMagick lurking?
I need to resize an image to fit into a 350x350 box. Which means I need to (a) scale the image so no dimension exceeds 350, then (b) centre the image in a 350x350 canvas.
Eg for a 700x600 image, (a) creates a 350x300 image which (b) then converts to 350x350 without distorting it (by adding "whitespace".
Ideally I want some control over what the canvas colour ("whitespace") will be, in particular transparent would be a good option, but white will do for now.
The first bit is easy: convert -resize 350x350 old.png new.png I've tried various ways to do step (b) without success. (A single command which does both would be best, but two separate commands would be a start!)
convert -border 2x0 -bordercolor #0000FF -resize 350x350 input.jpg -background #FFE000 -gravity center -extent 350x350 output.jpg
You might want to change the colours, though.
Bill Hill wrote:
convert -border 2x0 -bordercolor #0000FF -resize 350x350 input.jpg -background #FFE000 -gravity center -extent 350x350 output.jpg
That's just what I needed, thanks (I got there on most of it, but there's some extras in there I didn't know).
Any idea how to go for transparent backgrounds? (Aside from not using .jpg, of-course!)
Mark Rogers wrote:
Bill Hill wrote:
convert -border 2x0 -bordercolor #0000FF -resize 350x350 input.jpg -background #FFE000 -gravity center -extent 350x350 output.jpg
That's just what I needed, thanks (I got there on most of it, but there's some extras in there I didn't know).
Any idea how to go for transparent backgrounds? (Aside from not using .jpg, of-course!)
Replace "output.jpg" with "-transparent #FFE000 output.gif" (Might be -transparent-color ???) Where #RRGGBB is the same as the background. Bill
Bill Hill wrote:
Replace "output.jpg" with "-transparent #FFE000 output.gif" (Might be -transparent-color ???) Where #RRGGBB is the same as the background.
Think it's -transparency
What I don't want to do is change any part of the existing image, though, just make sure the surrounding area created by extending the canvas is transparent.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Mark Rogers wrote:
Bill Hill wrote:
Replace "output.jpg" with "-transparent #FFE000 output.gif" (Might be -transparent-color ???) Where #RRGGBB is the same as the background.
Think it's -transparency
What I don't want to do is change any part of the existing image, though, just make sure the surrounding area created by extending the canvas is transparent.
GIF transparency is supported by stealing one of the available palette entries (IIRC), out of the up-to 256 available. Therefore, I used to set the "background" to some hideous colour (like a lime green or bright purple) which was unlikely to occur in the rest of the image, and set that to to be the transparent colour.
If you convert to PNG, this isn't an issue as it supports proper alpha channels (i.e. variable opacity) thus transparency is simply a pixel of any colour with an alpha-value of 0x00, rather than one of its palette members being assigned "transparent" (or something vaguely like that).
Cheers, Simon
- -- - --------------------------------------------------------------------- Simon Ransome http://nosher.net
Photography RSS feed - http://nosher.net/images/images.rss
simon ransome wrote:
GIF transparency is supported by stealing one of the available palette entries (IIRC), out of the up-to 256 available. Therefore, I used to set the "background" to some hideous colour (like a lime green or bright purple) which was unlikely to occur in the rest of the image, and set that to to be the transparent colour.
Been there and done that, and been caught out when trying to generalise!
Looking through the convert options again, it seems this is handled with -transparent vs -transparent-color although I haven't quite worked out how to use them in practise - I found a useful thread here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=12914
If you convert to PNG, this isn't an issue as it supports proper alpha channels (i.e. variable opacity) thus transparency is simply a pixel of any colour with an alpha-value of 0x00, rather than one of its palette members being assigned "transparent" (or something vaguely like that).
How is the alpha value specified? Eg: If I want to set a colour as white I could use #FFFFFF, but how would I specify white with an alpha value of (say) 50%?