How to "center center" an image

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
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

How to "center center" an image

Post by ultranerds »

Hi,

I'm trying to thumbnail my images so they always make 300x300px, but are centered bother horizontally AND vertically. For the design of my site (in rows) I need to keep all the images the same size.

I've tried:

convert -define jpeg:size=200x200 7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg -thumbnail 200x200^ -gravity center -extent 200x200 test-7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg

..and this does make the thumb ok, but its only centered at the top of the image. The following shows an example of what I'm currently getting, and what I want:

http://mywebsitedesignersblog.com/what-i-want.png

Any ideas?

TIA!

Andy
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to "center center" an image

Post by fmw42 »

Your command should work. Perhaps you have an old version of IM that needs updating? What IM version and platform are you using?
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: How to "center center" an image

Post by ultranerds »

Mmm... this is what it says:

convert -version
Version: ImageMagick 6.5.8-8 2010-06-25 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP

Here is an example of what it outputs:

http://waaaant.com/uploads/large/40/tes ... Obz9Kl.jpg

...I think thats pretty up-to-date?

TIA
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to "center center" an image

Post by Bonzo »

Forget this post :(
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: How to "center center" an image

Post by ultranerds »

Sorry?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to "center center" an image

Post by Bonzo »

Your version is 2009 - I posted before I looked it up.

Looking back there was a problem with extent and gravity fixed at version 6.4.3.5

Have you tried it without the ^ and see what happens. I would try a very simple command first and see if something else is causing the problem.
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: How to "center center" an image

Post by ultranerds »

Thanks for the reply. If I remove ^ from the dimensions, I get the same image:

http://waaaant.com/uploads/large/40/tes ... Obz9Kl.jpg

:/
I would try a very simple command first and see if something else is causing the problem.
"simple" as in? :)

TIA

Andy
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: How to "center center" an image

Post by ultranerds »

Hi,

I got my host to upgrade me to the latest version they were comfortable with:

Code: Select all

Version: ImageMagick 6.7.6-7 2013-01-30 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Still got the same problem though :( Is there maybe another way of invoking IM to do what I want? I've only really dabbled with it (most of the time I just use it for straight forward "resize" facilities)

TIA

Andy
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to "center center" an image

Post by Bonzo »

Something simple would be ( where extent is bigger than your image ):

Code: Select all

convert7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg -background NavajoWhite -gravity center -extent 200x200 test-7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg
I have just noticed you do not have a background colour.

Are you using php or similar and when you tired "convert -version" was it through the code or via the cpanel?

I would guess you could have two versions of Imagemagick installed.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to "center center" an image

Post by fmw42 »

As Bonzo said, there may be two or more versions of IM installed in different locations. You may have to preface convert with the path to it. Ask you ISP what is the path to the latest version of convert that they just installed. Typically it is /usr/bin or /usr/local/bin. So perhaps by default, you are still going to the older version by leaving off the path to convert.
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: How to "center center" an image

Post by ultranerds »

Thanks for the replies guys. As far as I can tell its using the right one:
user@undevmac ~/waaaant.com/cgi-bin/admin/templates $ convert --version
Version: ImageMagick 6.7.6-7 2013-01-30 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features:

steveraf@user ~/waaaant.com/cgi-bin/admin/templates $ /usr/bin/convert --version
Version: ImageMagick 6.7.6-7 2013-01-30 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features:
A "whereis convert" gives me (as I would expect);

convert: /usr/bin/convert /usr/share/man/man1/convert.1.bz2

What command would you guys be using yourself? All it needs to do is:

1) Shrink to 200x200 (max)
2) This image then needs to be a fixed size of 200x200 (to fit in its placeholder on the site, which requires a fixed size)

TIA

Andy
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to "center center" an image

Post by Bonzo »

If your background is always the same you could composite your new image onto a standard background.

Code: Select all

convert -define jpeg:size=200x200 7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg -thumbnail 200x200^ background.jpg -gravity center -composite test-7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: How to "center center" an image

Post by ultranerds »

Thanks - that just gives me a white image? (I used an example "blank" image that is the correct size)

http://waaaant.com/uploads/large/40/tes ... Obz9Kl.jpg

Any ideas?

TIA

Andy
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to "center center" an image

Post by Bonzo »

I do had the images the in the wrong order - you will also need to use brackets now ( ) on windows \( \) on linux.

Code: Select all

convert background.jpg \( -define jpeg:size=200x200 7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg -thumbnail 200x200^ \) -gravity center -composite test-7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg
ultranerds
Posts: 41
Joined: 2009-02-12T02:05:15-07:00

Re: How to "center center" an image

Post by ultranerds »

Hi,

Wahooooo we got there! This works:

Code: Select all

convert blank300.gif \( -define jpeg:size=300x300 7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg -thumbnail 300x300 \) -gravity center -composite test10-7UFFBJ7XVryG2hEljnQH247FObz9Kl.jpg
Thank you everyone for your help 8) (you can see these nice new images on the homepage btw: http://waaaant.com/ ... if you resize the window you will see its responsive, which is why I needed the fixed width images, so it scaled right =))

Cheers

Andy
Post Reply