Search code examples
imagemagickcompressionjpeg

Optimize an image once and avoid doing it again


I have a large respository of images, mostly JPEG, which I'd like to optimize using a library like ImageMagick or a Linux CLI tool like jpegtran (as covered in JPG File Size Optimization - PHP, ImageMagick, & Google's Page Speed), but I don't want to have to track which ones have been optimized already and I don't want to re-optimize every one again later. Is there some sort of flag I could easily add to the file that would make it easy to detect and skip the optimization? Preferably one that would stay with the file when backed up to other filesystems?

E.g.: a small piece of exif data, a filesystem flag, some harmless null bytes added at the end of the file, a tool that is already intelligent enough to do this itself, etc..


Solution

  • You could use "extended attributes" which are metadata and stored in the filesystem. Read and write them with xattr:

    # Read existing attributes
    xattr -l image.png
    
    # Set an optimised flag of your own invention/description
    xattr -w optimised true image.png
    
    # Read attributes again
    xattr -l image.png
    optimised: true
    

    The presence of an extended attribute can be detected in a long listing - it is the @ sign after the permissions:

    -rw-r--r--@ 1 mark  staff  275 29 May 07:54 image.png
    

    As you allude in your comments, make sure that any backup programs you use honour the attributes before committing to it as a strategy. FAT-32 filesystems are notoriously poor at this sort of thing - though tar file or similar may survive a trip to Windows-land and back.

    As an alternative, just set a comment in the EXIF header - I have already covered that in this related answer...