Page 1 of 2
Rename image according to mean value
Posted: 2010-08-11T16:01:36-07:00
by forumware
Hi, just found this program, love it already but need help badly.
I have about 500 images, these have been all converted to grayscale. I would like to rename each image via a script with its Mean grayscale value:
Used the following command to get the value of each:
convert rose: -colorspace gray -format "%[fx:100*mean]%%" info:
but in order to do this i need to rename each file seperately to "rose" in the above command to make it work. Is there a way to traverse all the images and rename them according to their mean value or %.
Thanks in advance for your help.
Thanks,
Re: Rename image according to mean value
Posted: 2010-08-11T17:43:10-07:00
by fmw42
I believe you will have to write a loop over each of your images, compute the mean and store in a variable such as meanval, then append that to your output file, such as rose_${meanval}.png
I know of no shortcut or way to include the fx calculation within the output name in one command line. That is how I would do it in unix. For Windows users, see
http://www.imagemagick.org/Usage/windows/
Re: Rename image according to mean value
Posted: 2010-08-11T17:46:21-07:00
by magick
Assume a modern release of ImageMagick, you can embed properties into the output filename if you prefix the modifier with filename: (for security reasons). For example something like image_%[filename:...].
Re: Rename image according to mean value
Posted: 2010-08-11T17:56:19-07:00
by fmw42
This is very awkward but works in unix. I have no idea how to do it in Windows.
convert rose: -colorspace gray rose_$( convert rose: -colorspace gray -format "%[fx:100*mean]%" info: ).png
results in
rose_41.2302%.png
or
infile="rose.jpg"
inname=`convert $infile -format "%t" info:`
convert $infile -colorspace gray ${inname}_gray_$( convert $infile -colorspace gray -format "%[fx:100*mean]%" info: ).jpg
Re: Rename image according to mean value
Posted: 2010-08-11T18:05:26-07:00
by forumware
Thank you both for helping.
I need help with the loop, but was trying something like this, it's not working but is it syntax? Replace
mogrify -set filename:name %f -write '%[filename:name]_orig.gif' \
with
mogrify -set filename:name %f -write '%[filename:name]_%[fx:100*mean]%%info.jpg'
I'm on a mac so can you please give me the loop script to go over the images? I'm really in a bind...
Thanks in advance...
Re: Rename image according to mean value
Posted: 2010-08-11T18:16:40-07:00
by forumware
thank fmw,
I tried your command but substituted *.jpg for the rose
convert *.jpg -colorspace gray rose_$( convert rose: -colorspace gray -format "%[fx:100*mean]%" info: ).jpg
but i get image1_41.2302%.jpg, image2_41.2302%.jpg and so on, so i don't think the mean is actually being calculated because the number is constant.
Thanks,
Re: Rename image according to mean value
Posted: 2010-08-11T18:20:26-07:00
by fmw42
magick wrote:Assume a modern release of ImageMagick, you can embed properties into the output filename if you prefix the modifier with filename: (for security reasons). For example something like image_%[filename:...].
This is a puzzle to me!
If I do:
convert rose: -colorspace gray roseg.png
convert roseg.png rose_%[filename:fx:mean].png
The result is rose_%[filename/fx/mean].png on my Mac OSX tiger, IM 6.6.3.4 Q16 HDRI
Am I misunderstanding this issue about %[filename:...]
Note any colon I use there turns into a "/" as above.
Can you give an example of using that to put some fx calculation in an output name?
Re: Rename image according to mean value
Posted: 2010-08-11T18:30:31-07:00
by fmw42
forumware wrote:thank fmw,
I tried your command but substituted *.jpg for the rose
convert *.jpg -colorspace gray rose_$( convert rose: -colorspace gray -format "%[fx:100*mean]%" info: ).jpg
but i get image1_41.2302%.jpg, image2_41.2302%.jpg and so on, so i don't think the mean is actually being calculated because the number is constant.
Thanks,
You cannot do multiple input images with convert in this way as far as I know. And I doubt that mogrify is up to date with this feature using %[filename:...;] which is probably only in convert.
To do what I said, you will need to write a loop. If you only want jpg files, then try
cd yourdirectory
filelist=`ls | grep '.jpg'`
for infile in "$filelist"; do
inname=`convert $infile -format "%t" info:`
meanval=`convert $infile -colorspace gray -format "%[fx:100*mean]%" info:`
convert $infile -colorspace gray ${inname}_gray_${meanval}.jpg
done
Re: Rename image according to mean value
Posted: 2010-08-11T18:44:26-07:00
by forumware
Thanks a ton for your help, i tried the second set of commands and here is the output
convert: no decode delegate for this image format `40.033%' @ error/constitute.c/ReadImage/532.
convert: unable to open image `34.9604%': No such file or directory @ error/blob.c/OpenBlob/2514.
convert: no decode delegate for this image format `34.9604%' @ error/constitute.c/ReadImage/532.
convert: unable to open image `38.0018%': No such file or directory @ error/blob.c/OpenBlob/2514.
convert: no decode delegate for this image format `38.0018%' @ error/constitute.c/ReadImage/532.
convert: unable to open image `61.8306%': No such file or directory @ error/blob.c/OpenBlob/2514.
convert: no decode delegate for this image format `61.8306%' @ error/constitute.c/ReadImage/532.
convert: unable to open image `68.5519%': No such file or directory @ error/blob.c/OpenBlob/2514.
convert: no decode delegate for this image format `68.5519%' @ error/constitute.c/ReadImage/532.
I guess it's definately getting the mean value but not writing it to the filename, here are the filenames present in the directory,
55.4585%-12.jpg
55.4585%-268.jpg
image121.jpg
image27.jpg
55.4585%-120.jpg
55.4585%-269.jpg
image122.jpg
image270.jpg
55.4585%-121.jpg
55.4585%-27.jpg
image123.jpg
image271.jpg
55.4585%-122.jpg
Re: Rename image according to mean value
Posted: 2010-08-11T19:24:23-07:00
by fmw42
Not sure I understand why that does not work. But this actually works for me, whereas the previous way did not.
cd yourdirectory
filearray=(`ls | grep '.jpg'`)
num=${#filearray[*])}
for ((i=0; i<$num; i++)); do
inname=`convert ${filearray[$i]} -format "%t" info:`
meanval=`convert ${filearray[$i]} -colorspace gray -format "%[fx:100*mean]%" info:`
convert ${filearray[$i]} -colorspace gray ${inname}_gray_${meanval}.jpg
done
Re: Rename image according to mean value
Posted: 2010-08-11T19:32:19-07:00
by forumware
fmw42, my friend you are a genius... It worked perfect. Right on with the array. Thanks a ton.
Re: Rename image according to mean value
Posted: 2010-08-11T19:41:33-07:00
by anthony
magick wrote:Assume a modern release of ImageMagick, you can embed properties into the output filename if you prefix the modifier with filename: (for security reasons). For example something like image_%[filename:...].
See IM Examples, User labels in file names.
http://www.imagemagick.org/Usage/files/#save_escapes
So in your case
Code: Select all
convert rose: -set filename:mean "%[mean]" 'rose_%[filename:mean].png'
This created a file called "rose_27022.8.png" in my current directory.
You can use -fx expressions to modify the number, but at this time you can not format the number to
include things like leading zeros. At least not at this time. Maybe if an when -fx is replaced with a
properly interpreted API language that can handle and output strings. (EG: TCL, Perl, etc)
NOTE: user labels used in output filenames MUST be prefixed with "filename:" as a security measure.
Re: Rename image according to mean value
Posted: 2010-08-11T19:51:11-07:00
by fmw42
But this works:
convert rose: -set filename:mean "%[fx:100*mean]%" rose_%[filename:mean].png
creates
rose_57.142%.png
Now when -precision gets fixed with fx calcs, one should be able to no decimals
convert rose: -precision 0 -set filename:mean "%[fx:100*mean]%" rose_%[filename:mean].png
and get
rose_57%.png
I hope!
Anthony: Thanks for the clarification and example.
Re: Rename image according to mean value
Posted: 2010-08-11T20:37:48-07:00
by anthony
You can also use the 'int' function too
Code: Select all
convert rose: -set filename:mean "%[fx:int(100*mean)]%" rose_%[filename:mean].png
Looks like its does integer 'rounding' rather than 'clipping' - perhaps a bug?
That is int() is acting like round() rather than trunc() that is defined in the FX documentation and I would prefer it to do.
Re: Rename image according to mean value
Posted: 2010-08-11T20:43:03-07:00
by fmw42
Now if we can only get a -set filename:size and be able to use it in subsequent geometry.
