Efficient batch converting

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
naruto
Posts: 1
Joined: 2019-05-02T15:24:20-07:00
Authentication code: 1152

Efficient batch converting

Post by naruto »

Please kindly help me. I have been googling but couldn't find a really good script. Basically, I am trying to run:
1. imagemagick convert to 1170px over a folder, including subfolder but preserve modified date
2. but doesn't want to process/analyze every image, especially those that are smaller than or equal 1170px
3. skip processed images
4. run script every one hour.

I think for number 2, I google that I would need exiftool but doesn't have experience using it

Here is what I have so far:
crontab -e

Code: Select all

0 * * * * bash sharingan.sh >/dev/null 2>&1
sharingan.sh:

Code: Select all

#!/usr/bin/env bash

find Images -mmin -60 -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' \)|

while read file
do convert -resize 1170 "$file" "$file".tmp
   touch -r "$file" "$file".tmp
   cp -f -p "$file".tmp "$file"
   rm "$file".tmp
done
I still can't preserve the modification time. What do I do wrong?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Efficient batch converting

Post by fmw42 »

Imagemagick will only resize if larger than some dimension if using the > character. See https://imagemagick.org/script/command- ... p#geometry. If you are asking about total pixels, then you will need to get the WxH separately and then test on that.

I do not know if Imagemagick can preserve the modification date, since it is modifying the image or creating new ones. I do not know much about this, but you could check out -define png:include-chunk=value at https://imagemagick.org/script/command- ... php#define

You can process a whole folder of image using mogrify. But it will not traverse directories. So you are better off looping over your images from each/all directories that you want.
bratpit
Posts: 17
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Efficient batch converting

Post by bratpit »

Try something like this in your loop:

//get mtime and add to variable
mtime=$(stat -c %y "$file")
//process file
convert "$file"
//after that change mtime
touch -d "$mtime" "$file"
Post Reply