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)?
3 Answers
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.
exiftoolis used to find the images that need resizingconvertresizes them (doesn't touch the exif info)touch -rrestores the original timestamp from the backup file
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
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
jpegoptiminstead which includes a-poption 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.