2

I have a huge collection of family photos which I don't really need full resolution of. I want to use mogrify but I tried it on a sample set of images and it seems that it sets modification time of files to current time. Is there any way to preserve modification/create time of original image (for both EXIF data and linux's native file modification times which you can view using stat)?

1
  • maybe you can use jpegoptim instead which includes a -p option that preserves modification time. jpegoptim is good at reducing file size, but as the name suggests, only works for jpgs. For pngs, I use a workaround like below answer too. Commented Feb 2, 2019 at 14:25

3 Answers 3

3

You could always do it with a bit of scripting:

exiftool  -q -r -ext jpg -if '
    $ImageWidth > 1000 ||
    $ImageHeight > 1000 and
      !print "$Directory/$Filename\0"' . |
  xargs -r0 sh -c '
    for file do
      mv -i "$file" "$file.back" &&
        convert -resize "1000x1000>" "$file.back" "$file" &&
        touch -r "$file.back" "$file"
    done' sh

Here resizing the images so they fit into a bounding box of 1000x1000.

  • exiftool is used to find the images that need resizing
  • convert resizes them (doesn't touch the exif info)
  • touch -r restores the original timestamp from the backup file
3

You can use ImageMagick mogrify to down-res (resize smaller) any files larger than 800x800 to fit within 800x800 and preserve their timestamps like this:

magick mogrify -define preserve-timestamp=true -resize '800x800>' *.jpg

If you want to test first without overwriting originals, but instead writing the resized files to a new directory called RESIZED, you can do:

mkdir RESIZED
magick mogrify -path RESIZED -define preserve-timestamp=true -resize '800x800>' *.jpg
1

I'm not sure about ImageMagick, but GraphicsMagick supports this natively.

$ ls -lh family_photo.jpg 
-rw-r--r-- 1 justin justin 901K Sep  9  2007 family_photo.jpg
$ gm mogrify -preserve-timestamp -rotate -90 family_photo.jpg 
$ ls -lh family_photo.jpg 
-rw-r--r-- 1 justin justin 681K Sep  9  2007 family_photo.jpg

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.