Page 1 of 1

Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T07:25:58-07:00
by borskaegel
I am using mogrify to process JPGs in a directory, however I only want to target JPG files that are at least 100kb in size. Having this run on already small JPEG files in the target directory isn't desired. I've searched and most everything I've seen has been in relation to image dimensions. This is what I am currently using:

Code: Select all

find . -name "*.jpg" -exec mogrify -define jpeg:extent=150kb -interlace line -quality 70 -strip -type TrueColor {} \;
Any ideas?

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T10:05:25-07:00
by fmw42
create an if test on the file size either from the OS (-ls) or using convert image -format "%b" info: then pipe that filtered list of files to mogrify.

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T10:42:10-07:00
by borskaegel
I figured that might be the answer. This sounds simple as stated, however I am having issues getting it working as I am newer to ImageMagick. Mind giving me a hand on the actual implementation?

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T11:18:58-07:00
by fmw42
I am not an expert on unix and especially on find, but see the man pages on find for the -size option, which appears to allow a condition on the size. If that does not work, let me know and I can try to help work around that. My approach would be much more brute force involving several steps.

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T11:28:39-07:00
by borskaegel
Here's what I have so far. Running this is a bash window.

Code: Select all

if [ find . -name "*.jpg" -size +100000c ]; then -exec mogrify -define jpeg:extent=150kb -interlace line -quality 100 -strip -type TrueColor {} else -exec mogrify -interlace line -strip -type TrueColor {} \;
I cobbled that together from your suggestion and some suggestions I found on getting the size in bash. Getting a cursor and no results. See anything I need to change?

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T11:30:31-07:00
by fmw42
If it works, that is fine. I really do not know that much about find. I am surprised that it works with mogrify. But then again I am not that much of an unix expert. If it fails, then we try something else.

I had presumed that your find with -size would not need an if conditional. That is that -size would do a filter on the find.

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T11:43:33-07:00
by borskaegel
Ok, I removed the IF statement and I have this:

Code: Select all

find . -name "*.jpg" -exec mogrify -size 100000 -define jpeg:extent=150kb -interlace line -quality 100 -strip -type TrueColor {} \;
Maybe I'm just not seeing it, but how do I pass the actual file size in using -size? I tried what you see above, "-size 100000b" and nothing works. I get invalid argument as an error.

Feel free to post your solution if it seems we're going in circles here.

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T12:25:20-07:00
by fmw42
This is untested Unix bash shell syntax.


# find all files and put names into arr (won't work if file names have spaces)
# for each file in arr do a loop
# get file size from IM, but it is in Bytes with B at the end
# remove the B
# divide by 1000 to get KB
# test if file is larger than 100 KB
# if so use convert to process it and overwrite with the same name
# if you don't want to overwrite, then add something at the beginning of the output name such as "new_${arr[$i]}"
or put all the processed files in some pre-created empty directory, "path2/newdirectory/${arr[$i]}"

Code: Select all

arr=(`find . -name "*.jpg"`)
num=${#arr[*]}
for ((i=0; i<num; i++)); do
	size=`convert "${arr[$i]}" -format "%b" info:`
	size=`echo $size | sed -n 's/^\([0-9]*\)B$/\1/p'`
	size=`echo "scale=0; $size/1000" | bc`
	if [ $size -ge 100 ]; then
	convert "${arr[$i]}" -define jpeg:extent=150kb -interlace line -quality 70 -strip -type TrueColor "${arr[$i]}"
	fi
done

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-29T20:13:10-07:00
by snibgo
borskaegel wrote:Ok, I removed the IF statement and I have this:

Code: Select all

find . -name "*.jpg" -exec mogrify -size 100000 -define jpeg:extent=150kb -interlace line -quality 100 -strip -type TrueColor {} \;
Maybe I'm just not seeing it, but how do I pass the actual file size in using -size? I tried what you see above, "-size 100000b" and nothing works. I get invalid argument as an error.
You are passing "-size" as an argument to mogrify. You should be passing it as an argument to find. For example:

Code: Select all

find . -name "*.jpg" -size +100000c -exec echo {} \;
Suffix "b" is for blocks. I think you want "c".
(This is with Gnu bash from Cygwin under Windows 8.1.)

Re: Target images over a certain file size (kb, etc.)

Posted: 2014-07-30T05:29:50-07:00
by borskaegel
That did it! Thanks to you both. :)