?
Solved

ImageMagick: How resize image to certain width with no pixelation (shrink but not grow)?

Posted on 2011-05-04
4
Medium Priority
?
1,501 Views
Last Modified: 2013-11-19
I bet ImageMagick can do this but Googling hasn't turned up any easy answers:

Given a source image I want to scale an image DOWN to 600 pixels across but I don't want the image to grow up to 600px.

Examples:
   800px width -> 600px
   600px width -> 600px
   400px width -> 400px (leave at 400... don't pixelate)

Here is my current command line which pixelates:
convert input.jpg -thumbnail 600 output.jpg
What option(s) can I pass to accomplish this?
0
Comment
Question by:ZuZuPetals
  • 3
4 Comments
 
LVL 39

Accepted Solution

by:
BillDL earned 2000 total points
ID: 35706720
In ImageMagick (as with most image viweres and editors) the dimensions of an image are specified as Width x Height in that order.  X = Width, Y = Height.  In the ImageMagick help file you are looking for the "image geometry" related parameter:  -resize which you will see discussed at length in your help file:

command-line-processing.html#geometry

or online here:
http://www.imagemagick.org/script/command-line-processing.php#geometry

In most cases, unless you deliberately force it using operators, the geometry-related parameters maintain the aspect ratio of the original image being converted.

You can use the "less than" operator < with your new dimensions when using -resize to ensure that the image will only be resized if the new dimensions are smaller than the original dimensions you specify (ie. only reduce, don't enlarge). Conversely, the "greater than" operator > means "resize only if the new dimensions are greater than the original dimensions", ie. "only enlarge".

Using an 1600 x 1200 image as the source, the following command should work:

convert InFile.jpg -resize "x600>" OutFile.jpg

Verify that on a couple of test images smaller than 800 x 600 and larger than 800 x 600 and ensure that it works.

You don't always need to quote the dimensions in your -thumbnail or -resize command, but if using any operators like > you must enclose them AND the operator in quotes.
0
 
LVL 39

Expert Comment

by:BillDL
ID: 35706850
Whoops!! REVERSE that > operator to <

The operator is referring to the NEW dimension, not to the dimensions of the original image.  So "greater than" > means "if the NEW dimensions are greater than the original", and the "less than" < means "if the NEW dimensions are less than the original".

Sorry.

Just in case you had expected to see two dimensions specified in the above command eg. "800x600", it is explained in the helpfile that you don't always need both.  In this case I only used the Width dimension and < operator) "x600<" so that large images will be downsized to 600 pixels ON THE WIDTH while scaling the height down to maintain the original aspect ratio

0
 
LVL 2

Author Closing Comment

by:ZuZuPetals
ID: 35706945
Thanks!

I found that 600> is the correct geometry.  I think it is > and not <.

Also, I want to enforce width so the "x" is wrong (x implies re-size based on height).
0
 
LVL 39

Expert Comment

by:BillDL
ID: 35707304
Yes, sorry about that.  I haven't been too good at multi-tasking today.  Tired out and brain-dead.  Thanks for correcting.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a previously published article (http://www.experts-exchange.com/articles/10331/Automatic-Duplex-Scanning-in-PaperPort-Versions-11-12-14.html) here at Experts Exchange, I explained how to achieve duplex (double-sided) scanning in Nuance's PaperPor…
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
Make a selection using the pen tool to trace the selection. Then alter the color of the selection by using the color balance option in Adobe Photoshop.
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question