0

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..

fmw42
  • 46,825
  • 10
  • 62
  • 80
ColinM
  • 13,367
  • 3
  • 42
  • 49
  • In Imagemagick, you can add a comment to the image. `convert image -set comment "optimized" image. But even doing that would change the image if you save again as jpg, since each time it is read and written it goes through a lossy compression. You can probably do what you want without reading and writing the image data using EXIFTOOL and some other optimization tool such as jpegtran. – fmw42 Mar 30 '18 at 16:17
  • Thanks, reading further about exiftool I do see that it supports reading/writing it for PNG images as well as JPG so that would probably be the most future-proof solution, to use exif as the storage medium for the "already compressed" flag. – ColinM Apr 02 '18 at 19:26

2 Answers2

1

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...

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks! I used exiftool, but had I known about xattr already I probably would have used that instead. – ColinM May 31 '18 at 04:06
1

To piggyback off of Mark Setchell's answer, if you use xattr you'll most likely need to use a trusted namespace, otherwise you're likely to get an "Operation not supported" error. Most of the documentation I could find referred to "setfattr", however, the same trusted namespace rules apply to "xattr" as well.

For example, using the user namespace:

# Set an optimised flag of your own invention/description
xattr -w user.optimised true image.png