Rename image according to mean value
Rename image according to mean value
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,
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,
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Rename image according to mean value
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/
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
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:...].
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Rename image according to mean value
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
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
Last edited by fmw42 on 2010-08-11T18:37:19-07:00, edited 1 time in total.
Re: Rename image according to mean value
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...
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
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,
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,
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Rename image according to mean value
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?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Rename image according to mean value
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
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
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Rename image according to mean value
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
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
fmw42, my friend you are a genius... It worked perfect. Right on with the array. Thanks a ton.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Rename image according to mean value
See IM Examples, User labels in file names.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:...].
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'
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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Rename image according to mean value
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.
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.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Rename image according to mean value
You can also use the 'int' function too
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.
Code: Select all
convert rose: -set filename:mean "%[fx:int(100*mean)]%" rose_%[filename:mean].png
That is int() is acting like round() rather than trunc() that is defined in the FX documentation and I would prefer it to do.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Rename image according to mean value
Now if we can only get a -set filename:size and be able to use it in subsequent geometry. 
