How to rotate a *.jpg picture by 90 deg?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
bensto
Posts: 31
Joined: 2012-07-02T00:32:10-07:00
Authentication code: 13

How to rotate a *.jpg picture by 90 deg?

Post by bensto »

Assume I have a *.jpg picture

D:\mypicture\holidayjune\pic123.jpg

How can I lossless rotate this picture by 90 clockwise and save it as

D:\mypicture\holidayjune\pic123rot.jpg

?

What do I have to change in the previous command when I want to rotate it couter clockwise by 90 deg?

Ben
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to rotate a *.jpg picture by 90 deg?

Post by Bonzo »

I do not think you can do a lossless rotate with IM.

clockwise -rotate 90
anticlockwise -rotate 270 ( -90 may work )
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to rotate a *.jpg picture by 90 deg?

Post by fmw42 »

Even if the rotate 90 were lossless, just reading in a jpg and writing it out again would have some losses. JPG is a lossy compression image format. IM must read the input, decompress, recompress and write the ouput even to just copy the image. Other non-im solutions may be available. see

http://www.imagemagick.org/Usage/formats/#jpg_non-im
http://www.imagemagick.org/Usage/formats/#jpg_lossless
Last edited by fmw42 on 2012-07-02T11:19:49-07:00, edited 1 time in total.
User avatar
Dane Vandeputte
Posts: 14
Joined: 2012-07-01T18:26:53-07:00
Authentication code: 13
Location: Illinois, USA

Re: How to rotate a *.jpg picture by 90 deg?

Post by Dane Vandeputte »

fmw42 wrote:Even if the rotate 90 were lossless, just reading in a jpg and writing it out again would have some losses. JPG is a lossy compression image format.
Actually, as it turns out, one can losslessly rotate by multiples of 90 degrees (or transpose) a JPEG file by doing nothing more than rearranging and/or changing signs of the quantized DCT coefficients in each block.

In reply to Ben, you can use the freely available jpegtran.

Use the following command to rotate input.jpg 90 degrees clockwise and save the result to output.jpg:

jpegtran -rotate 90 input.jpg output.jpg

If the input image dimensions are not multiples of the JPEG block size, you may notice a small, undesirable border along some portions of the image. To eliminate the border, use the following command instead:

jpegtran -rotate 90 -trim input.jpg output.jpg

In either case, if you want to rotate 90 degrees counter-clockwise, replace the 90 in either command with 270. Also, here is the jpegtran manual for further reference.
Digital image processing and photography enthusiast :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to rotate a *.jpg picture by 90 deg?

Post by anthony »

See...IM examples, Photo Handling for just a quick answer.
http://www.imagemagick.org/Usage/photos/#orient

For a complete answer, see...
Common Image file Formats, Lossless JPEG Processing (non-IM)
http://www.imagemagick.org/Usage/formats/#jpg_lossless
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply