Lossless JPG Rotation

Ken Krugh 116 Reputation points

Working in VB.net I want to rotate JPGs lossless and reset the EXIF orientation flag to 1, so that when I'm done with the image any readers that ignore the EXIF flag will still show the image in the proper orientation. I've got the EXIF part down but I'm leery of the rotation.

I followed this Microsoft page that uses the metadata. The page's remarks remarks SAY "The rotation represents a lossless transformation of the source image..." but using this code as-is the image is DEFINATELY modified. I can clearly see the differences in the bits in Photoshop and the size of the file is greatly reduced. Changing "encoder.QualityLevel = 30" to "encoder.QualityLevel = 100" (the max) there doesn't APPEAR to be any differences in the bits but the file is still getting a bit smaller, though not significantly.

I've also tried using System.Drawing.Imaging similar to this page and again, the I'm not readily seeing any differences in Photoshop but the file is getting a bit smaller. Another disadvantage to this method is that it is also removing sizing data of some sort because when I open it back up in Photoshop instead of Photoshop showing it as a 3.343" x 4.915" image at 600 dpi, it's showing it as 27.861" x 40.958" at 72 dpi. The resolution hasn't changed but the "physical size" has.

Both methods seems to execute about the same speed, which makes sense as I THINK they're doing similar functions but the file size changing has me concerned that something I'm not seeing maybe happening to the image.

Can anyone shed some light on either of these methods and why the file size might be changing?

Many thanks,

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,838 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 65,791 Reputation points

    ... why the file size might be changing?

    You can see this article which explains it : Lossless rotation of JPEG images: There's more than one way to rotate a cat

    No comments

  2. Ken Krugh 116 Reputation points

    Great article, Castorix, thanks very much. After many frustrating hours I have to put this down until I get more time to investigate.

    For anyone interested: After testing more files from 2 Canon cameras, a Samsung S21 and an Epson FF-680W scanner and comparing things before and after using Photoshp, I find that results vary greatly depending on the original file. Which makes sense given the DEcoding and ENcoding that has to happen. For instance, I found that using EXIF method mentioned in the original post, the files from the cameras had no changes at all. But using the System.Drawing method quite a few of the pixels had different RGB values after the rotate. These differences were only a few units up or down, but still there. This could also be a result of something Photoshop is seeing, or something now missing, after the rotate. Original image is on the left, the right is the Photoshop comparison, colored areas are differences, black areas are the same.

    On an image from the S21 the EXIF method caused about same changes as above, but the changes were much worse on the S21 photo using the System.Drawing method:

    So, I've decided that for now I'm going to try and emulate what the Explorer does in Windows 10 does when you right click and say rotate right or left. Which, I'm nearly sure is just updating the EXIF data.

    BUT, when I modify just the Orientation flag it's OK in Windows, the vb.net app I'm writing that displays it (both Microsoft, of course), but not in Photoshop. Photoshop is OK when Explorer rotates it, so I've got more investigating to do.

    If anyone can cast any light on any of the above it'll be MUCH appreciated. When I can get back to this, if I find anything new I'll post back.


    No comments