14

When an image is rotated by convert -rotate command the image size is enlarged. Is there a way to rotate around the center and to keep the image size, cropping the edges?

23
convert image.jpg -distort SRT -45 rotate_cropped_image.png

See http://www.imagemagick.org/Usage/warping/#animations

Example: Animated GIF

See also help on -distort: http://www.imagemagick.org/script/command-line-options.php?#distort

6
  • result is not what a user would expect from rotating an image. – scrat.squirrel Jan 15 '17 at 1:08
  • Since the image dimensions are the same and the colored region inside the image area is the same, the file size should be the same, shouldn't? I tried this on a binary image having 13kb and the result has more than 20kb. – Sigur Mar 17 '17 at 23:54
  • @Sigur Wrong. Animated gif use some sort of compression. For example it can use only difference between frames. It is cause different size for different frame order. If you try convert usual gif to animated gif then size increased because need store every frame. – Enyby Mar 18 '17 at 15:05
  • @Enyby, sorry. My mistake. I was talking about rotating a single image file. No gifs. – Sigur Mar 18 '17 at 15:14
  • 1
    @Sigur All same for rotate any compressed image. Only bmp do not compressed. All other format use compression. Compression can give different result even if all pixels stay same but reordered. This is flip case. In all other cases pixels changed - any rotation on custom angle change all pixels. It cause different size. It can be smaller or larger. – Enyby Mar 18 '17 at 15:35
1

This seems now to simply "just work" -- for counter-clockwise 90 degrees:

$ convert image.jpg -rotate -90 rotated_ccw.jpg
1
1

If you know the size of the image the following works:

convert -rotate 45 -gravity center -crop NxN input output

tested with square images. there may be a way to specify NxN is the input image size.

0

I've found this answer on Imagemagick forum:

A simple solution without knowing what the original size of the image was, is to use the Alpha Composite Operator 'Src' as a 'crop to this image size' type of operation. See:

http://www.cit.gu.edu.au/~anthony/graphics/imagick6/compose/#src

For example (ImageMagick version 6 only):

convert image.jpg \( +clone -background black -rotate -45 \) \
    -gravity center -compose Src -composite rotate_cropped_image.png
1

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.