Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

imagemagick - Rotate and crop an image, while keeping its original size

I have a JPG image with a known size of 3072x2048. Now I want to rotate that image by any degrees (e.g. 45), while keeping its original size. Thus - using ImageMagick on the command line - I first want to rotate, then crop the image, like this:

convert -rotate 45 -gravity center -crop 3072x2048 +repage original.jpg rotated-45.jpg

By using -gravity center I specify to crop the center part of the image, which is what I want. This operation produces four output images:

  • rotated-45-0.jpg
  • rotated-45-1.jpg
  • rotated-45-2.jpg
  • rotated-45-3.jpg

The first image rotated-45-0.jpg is exactly the final image I want to get. The other three I don't need. I could delete them, but I think it would be nicer to not generate these "extra" images in the first place. So I thought I could do it with this command instead:

convert -rotate 45 -gravity center -crop 3072x2048+0+0 +repage original.jpg rotated-45.jpg

This only produces one output image, however, now the top-left corner of the image is being cropped. So apparently the -gravity center is not used any longer.

Any ideas what I am missing here?

question from:https://stackoverflow.com/questions/66048971/rotate-and-crop-an-image-while-keeping-its-original-size

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using ImageMagick you can rotate an image any number of degrees while keeping the original canvas dimensions using "-distort SRT"...

convert original.jpg -virtual-pixel black -distort SRT 45 rotated-45.jpg

Use "-virtual-pixel" to specify how you want to handle the parts that were outside the canvas before the rotation. In this example I used "black". You can use black, white, background, tile, mirror, or none.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...