Say I have a directory with a 1000 item image sequence. If I only want to have ImageMagick convert every other file, how can I do that?
I'm using this command to convert all files in a directory, which works fine:
convert /files/*.dpx -resize 50% small_%04d.jpg
But I'd like to be able to specify how many of the files to convert. Every other file (001, 003, 005, etc.) would be great but it would also be nice to convert just a certain number of files in any directory for an average of sorts. For instance, if I have a directory with 6,232 files and I only want to convert 720 of them (spread out or averaged across the sequence range) how do I go about doing that?
Thanks!
Matt
convert a subset of files in a directory
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: convert a subset of files in a directory
you would likely need to write a script with a loop over your images and some skip factor. IM does not have a skip factor as far as I know, even with frames or clones.
Re: convert a subset of files in a directory
OK. That makes sense.
So, I need to dig into shell scripting, eh? I'll see if I can manage to fumble my way through. The script would need to do something like this:
1. Determine number of files in an image sequence in a given directory
2. If there are more than 720 files, divide the actual number of files by 720 and round to nearest integer
3. Use the result as a "skip n files" to loop through the file list to send to ImageMagick
So, if I ran the script on a directory with 10,960 images in it, I would want the script to process every 15th file in the sequence. There would be some slop because of the rounding (in this example, about 10 files extra) but I could truncate the processed files easily enough, I think.
Just thinking out loud here.
-Matt
So, I need to dig into shell scripting, eh? I'll see if I can manage to fumble my way through. The script would need to do something like this:
1. Determine number of files in an image sequence in a given directory
2. If there are more than 720 files, divide the actual number of files by 720 and round to nearest integer
3. Use the result as a "skip n files" to loop through the file list to send to ImageMagick
So, if I ran the script on a directory with 10,960 images in it, I would want the script to process every 15th file in the sequence. There would be some slop because of the rounding (in this example, about 10 files extra) but I could truncate the processed files easily enough, I think.
Just thinking out loud here.
-Matt
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: convert a subset of files in a directory
It would certainly be nice if IM had a -skip N for use in mogrify! (And for use in selecting frames). Post a suggestion for that on the developers forum if you want.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: convert a subset of files in a directory
if you have the "seq" command (macs don't) you can do this to generate skipping numbers.
Just replace the echo with the command you want.
Or you can use this replacement for "seq" shell function...
# seq_integer format [start] [incr] end
Code: Select all
for i in `seq -f "%03g" 2 2 50`
do
echo $i
done
Or you can use this replacement for "seq" shell function...
# seq_integer format [start] [incr] end
Code: Select all
seq_integer() {
if [ "X$1" = "X-f" ]
then format="$2"; shift; shift
else format="%d"
fi
case $# in
1) i=1 inc=1 end=$1 ;;
2) i=$1 inc=1 end=$2 ;;
*) i=$1 inc=$2 end=$3 ;;
esac
while [ $i -le $end ]; do
printf "$format\n" $i;
i=`expr $i + $inc`;
done
}
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: convert a subset of files in a directory
OK, I think I got it (and yes, this is on a Mac so seq is not available).
The file skipping works by sending the list of files (ls) to awk to filter/skip lines. Then I send the result of that to "sort" for natural counting order (if dealing with large image sequences).
You can change the number of files skipped by adjusting the awk part (in the above script, the number "15"). Also, I used sort because I had an image sequence of nearly 11,000 items so I needed them to stay in natural counting order. The number "23" tells it to look at the 23rd character in the file name to do the sorting (I had a somewhat long file name preceding the sequence numbers in my file name).
Anyway, it appears to work for my needs. I might go back and add user input to it for the number of files to skip and the sort character number so that I don't have to adjust the script each time I use it but not today.
-Matt
Code: Select all
#!/bin/bash
LIST="$(ls | awk '{nr++; if (nr % 15 == 0) print $0}' | sort -n -k1.23)"
for i in "$LIST"; do
convert $i -resize 1x480\! ~/Desktop/barcodes/barcode_%04d.jpg
done
You can change the number of files skipped by adjusting the awk part (in the above script, the number "15"). Also, I used sort because I had an image sequence of nearly 11,000 items so I needed them to stay in natural counting order. The number "23" tells it to look at the 23rd character in the file name to do the sorting (I had a somewhat long file name preceding the sequence numbers in my file name).
Anyway, it appears to work for my needs. I might go back and add user input to it for the number of files to skip and the sort character number so that I don't have to adjust the script each time I use it but not today.
-Matt