36

How to create square/cropped thumbnail using ImageMagick ? Cropping like below.

cropping

8 Answers 8

23

This is explained in the official ImageMagick documentation under, “Cut the Thumbnail to Fit”:

An alternative, is rather than pad out the image to fit the specific thumbnail size we want, is to instead cut off the parts of the image that does not fit the final size.

Of course this means you actually lose some parts of the original image, particularly the edges of the image, but the result is a enlarged thumbnail of the center part of the image. This is usually (but not always) the main subject of the image, so it is a practical method of thumbnail creation.

As of IM v6.3.8-3 the special resize option flag '^' was added to make this easier. We just resize using this flag then crop off the parts of the image that overflows the desired size.

And in the context of an example command:

convert -define jpeg:size=200x200 hatching_orig.jpg  -thumbnail 100x100^ \
          -gravity center -extent 100x100  cut_to_fit.gif
2
58

Ignacio linked to the correct documentation, however I'll paste it inline here for convenience:

convert -define jpeg:size=200x200 original.jpeg  -thumbnail 100x100^ -gravity center -extent 100x100  thumbnail.jpeg

Similarly, the following is for GraphicsMagick:

gm convert -size 200x200 original.jpeg -thumbnail 100x100^ -gravity center -extent 100x100 +profile "*" thumbnail.jpeg

Explanation:

  • -size 200x200 tells the jpeg decoder we only need this resolution so it can save memory and read the source image faster
  • -thumbnail 100x100^ fast resize making the shortest side 100
  • - gravity center center the next operation
  • -extent 100x100 apply the image to a 100x100 canvas
  • +profile "*" do not save any metainfo to the jpeg (making the resulting image smaller)
4
  • 3
    When I used it in command line (graphics magick one) I got picture on center, two siders are filled with white. Not the one described in question. Am I doing something wrong? Jan 24, 2012 at 10:25
  • 2
    If you are using windows you must double the ^ character to escape it. Eg: -thumbnail 100x100^^ Jul 23, 2013 at 8:39
  • 3
    Why not size 100x100 ? Oct 19, 2014 at 10:42
  • @HelloWorld: Read "Imagemagick Geometry". Google it, it will be easy find. Jan 17, 2017 at 21:53
11

That is a simpler way to do it:

The following command resize the smaller side to 100 pixels and crop a 100x100 square. You can add a -strip command to reduce file size.

convert original.jpg -resize "100^>" -gravity center \ 
                     -crop 100x100+0+0 -strip thumbnail.jpg

Unlike others, it isn't trying to save memory. Instead, it does what you want, and no more. Plus, it won't upscale images.

2
  • When adding a late answer to a question with established answers it is helpful to provide enough explanation why your answer offers something unique and new compared to the other answers. May 31, 2014 at 5:49
  • 2
    Thanks, I don't know why the others overcomplicated their answers to try to save memory when OP mentions nothing at all about this.
    – confused00
    Sep 1, 2016 at 14:42
2

I'm using graphicsmagick to generate precisely sized thumbnails but I'm padding the image with a checkerboard instead of cropping the overhang.

gm convert -limit Threads 1 -size 320x180 pattern:checkerboard -background transparent -gravity center -resize 320x180 -extent 320x180 original.jpg -flatten -resize 112x65! -interlace Line 1 thumb_112x65.jpg

Options explained.

gm convert

// Single threaded seems faster on smaller files
-limit Threads 1 

// Generate a checkerboard of size 320x180.
// Sets the relative size of the checkerboard squares,
// also sets the desired aspect ratio. In my case (16:9)
-size 320x180 pattern:checkerboard 

// Resize the input image and center it on a transparent layer.
-background transparent -gravity center -resize 320x180 -extent 320x180 orig.jpg

// Merge the layers
-flatten 

// Resize the output to the desired
// The ! causes the aspect ratio to be ignored, fixing any rounding errors.
// (Specify a size with the same aspect ratio as the checkerboard.)
-resize 112x65! 

// Use Progressive JPEG Encoding
-interlace Line 

// Output Image
thumb_112x65.jpg
1

I think you are looking for something like:

convert -crop 100x100+50+50 input_image.jpg output_image.jpg 

where 100x100 is the size of the final rectangle and 50x50 the offset.

2
  • That crop image, but doesn't make thumbnail.
    – marioosh
    Apr 26, 2011 at 11:11
  • 1
    @marioosh: I didn't understood, you can use convert -thumbnail 100x100+50+50 input_image.jpg output_image.jpg, for more info check Igancio Vazquez response for an example or check the documentation Apr 26, 2011 at 11:15
1

This command crop to a square and then resize to 150x150

convert 824-full.jpg -set option:size '%[fx:min(w,h)]x%[fx:min(w,h)]' xc:none +swap -gravity center -composite -resize 150x150  temp.jpg

More options are availble here: http://www.imagemagick.org/Usage/thumbnails/#square

0

To create a batch of thumbnails you also could use mogrify

mogrify -path ./new/ \
        -resize 733x733^ \
        -gravity Center  \
        -extent 733x733  \
        ./*

this converts all .jpgs in the current directory and copies them to the ./new directory.

0

The command I use for 100 pixel square thumbnails is (all on one line): convert input.jpg -thumbnail x200 -resize '200x<' -resize 50% -gravity center -crop 100x100+0+0 +repage -format jpg -quality 91 square.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.