New gift: toolbar script for lossless rotation of JPG images

Hello to all,

I had noticed for some time now that rotating JPG pictures in DEVONthink would lower their file size, and quite obviously their quality. For instance a 2.5MB high resolution photo scan would become 700KB, which is quite unacceptable if you use DEVONthink to store and manage archival pictures.

The result is exactly the same when using Image Events in Applescript because both DEVONthink and Image Events appear to use the OS X Core Image library. This library probably does not use lossless rotation because it must work with any image format (png, gif, etc. do not feature orientation tags) and because all other functions have to modify the image data.

Another reason lossless rotation depends on the interpretation of the program that opens the image, so they prefer not to do it, even though the vast majority of programs will respect the orientation information in a picture. Finally, if anyone else does it, why not do it the same way: almost all picture editing programs — even Photoshop — end up recompressing the JPG after each modification that is saved to the disk.

Until now, I was using Preview whenever possible to rotate because the file size did not seem to change much, but today I realized it still changed, potentially losing quality, though much less.

So I created a script that uses ExifTool to alter only the EXIF orientation tag and not the image data at all. ExifTool must be installed on the system, which is as simple as launching the pkg found in the dmg downloadable there:
Test it, the file size only changes by a few bytes (because the metadata is rewritten).

You can put those scripts in the Toolbar subfolder of DEVONthink’s Scripts folder, and drag and drop them to your toolbar. The scripts only work for pictures, not PDF pages. You can run them on several pictures. If you run them on non-JPEG pictures, it will do a standard Core Image rotation.

Please leave a brief message on this thread if you use it! :smiley:

Best regards,
[Ed] Turn left and right (lossless for JPG).zip (33.1 KB)

Thanks for the offering. Do note though, this trick may lead to undesirable consequences when viewing the file in different contexts. The orientation flag is not read or used by all apps in all contexts.

Rotation of the pixel data is the more sure way to guarantee a desired view. Also, even if images are square, the compression algorithm used will yield different byte values due to the redistribution of pixels, post-modification. When the aspect ratio ≠ 1:1, the image size could even increase due to this redistribution. So you can’t judge the quality of a modified image by a change in file size as the larger size usually implies “better” quality. Inspection is the only way to determine post-modification quality.

Also, note that resaving a JPEG file will always degrade an image, even if by a small amount each time using Maximum quality. Depending on the particular needs, TIFF files are a better option for preserving quality. LZW compression (a run-length encoded, lossless algorithm) can be used if compression is needed. JPEGs can be created on an ad-hoc basis for other applications, as needed. Just a suggestion.

Cheers! :smiley:

What I’ve used in these cases is a program called jpegtran. Instead of changing the orientation flag, it changes the coefficients of the discrete cosine transformation in the JPEG data. This is also lossless, and it is compatible with pretty much every JPEG reader out there.

Interesting approach to the problem.

jpegtran is a good approach as well, it’s the only other lossless method and is compatible with all JPEG readers since the image is entirely rewritten. However, I have yet to find an application on Mac that doesn’t use the orientation flag and it’s slightly faster since only the metadata is rewritten.

Using the orientation flag does not rewrite the JPEG or lower the quality, but of course if further modification than rotation is due JPG will lose quality after each resave (except coefficients translation). However if further modification is not expected, I think it remains the best format for most non-critical applications because LZW-compressed TIFF takes CPU power when decompressing and remains much larger than JPG.


Hi guys, sorry for reviving this old topic. This is my first post here so sorry if I’m doing something wrong.

I’m trying to rotate a bunch of PNG images without success.

  • Using the menu bar (Tools > Rotate) does not “stick”, if I move away and return the file goes back to the wrong orientation.
  • I tried the script (Script > Images > Rotate) but it’s not working with this files - not sure what’s going on. Other images are working fine, both for JPG and PNG.
  • Rotating in Finder works

I wanted to try the script mentioned by OP, but I’m receiving an error when downloading it.

Does anyone know why rotating using the menu bar is not permanent? Also, why the script might not be working with this PNGs?


Welcome @ajmiranda

If you could compress a few of the files and upload them, that would be helpful.
If they’re private, you could start a support ticket and attach them, and we’ll handle them in our support system.

Hi @BLUEFROG. Thanks!

I’m a new user so I can’t upload directly to the forum or post a wetransfer link. I guess I’ll have to start a ticket

Ahh, yeah. Ticket’s good. :slight_smile:

Just if someone has the same problem, I’ve got a reply from my ticket:

" I have confirmed this is an issue in Mojave (and no, I don’t suggest moving to Catalina to resolve this issue yet. Catalina in a mess in other ways, so I’d suggest holding off until 10.15.3 is released) .
I will send this to Development for investigation. Thanks for your patience and understanding."