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

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
borskaegel
Posts: 5
Joined: 2014-07-29T07:21:47-07:00
Authentication code: 6789

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

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
borskaegel
Posts: 5
Joined: 2014-07-29T07:21:47-07:00
Authentication code: 6789

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

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
borskaegel
Posts: 5
Joined: 2014-07-29T07:21:47-07:00
Authentication code: 6789

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

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
borskaegel
Posts: 5
Joined: 2014-07-29T07:21:47-07:00
Authentication code: 6789

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.)
snibgo's IM pages: im.snibgo.com
borskaegel
Posts: 5
Joined: 2014-07-29T07:21:47-07:00
Authentication code: 6789

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

Post by borskaegel »

That did it! Thanks to you both. :)
Post Reply