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?
4 Answers
convert image.jpg -distort SRT -45 rotate_cropped_image.png
See http://www.imagemagick.org/Usage/warping/#animations
Example:
See also help on -distort: http://www.imagemagick.org/script/command-line-options.php?#distort
-
result is not what a user would expect from rotating an image. Jan 15, 2017 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.– SigurMar 17, 2017 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.– EnybyMar 18, 2017 at 15:05
-
@Enyby, sorry. My mistake. I was talking about rotating a single image file. No gifs.– SigurMar 18, 2017 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.– EnybyMar 18, 2017 at 15:35
This seems now to simply "just work" -- for counter-clockwise 90 degrees:
$ convert image.jpg -rotate -90 rotated_ccw.jpg
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.
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
-
image is rotated, but top and bottom from original is now cut off... not a good result Jan 15, 2017 at 1:09