> So rotating the image data could involve re-compressing the image again, leading to quality loss.
With JPEG, no, 90 degree rotations can be accomplished in a lossless manner.
See jpegtran from the libjpeg library. A version of its manpage is here:
https://linux.die.net/man/1/jpegtran
Quoting:
"... It can also perform some rearrangements of the image data, for example turning an image from landscape to portrait format by rotation.
jpegtran works by rearranging the compressed data (DCT coefficients), without ever fully decoding the image. Therefore, its transformations are lossless: there is no image degradation at all, which would not be true if you used djpeg followed by cjpeg to accomplish the same conversion. ..."