Page 1 of 1

Looping through folders and output convert to txt file with filename

Posted: 2018-09-24T02:22:26-07:00
by RobShawUK
hi

I have a large number of images in nested folders. I would like to loop through the images (they are small pngs) and capture the average colour of the image and write to a text file. I've tried doing this with

magick convert *.png -resize 1x1 all.txt

which works but doesn't include the filename (so I know which line in the txt file is which image) and doesn't recurse through folders.

Does anyone know if this is possible with Imagemagick please? Ideally the output would be something like

Path;RGB
c:\test\image1.png;(100,50,200)
c:\test\image2.png;(106,20,103)
etc

Thanks
Rob

Re: Looping through folders and output convert to txt file with filename

Posted: 2018-09-24T04:24:19-07:00
by snibgo
What version IM, on what platform? I assume v7.something, on Windows.

To get the average colour, use "-scale" to 1x1, not "-resize".

For v7, I suggest you use "magick", not "magick convert".

As you want a particular text format, including the filename, I suggest you use "-format", and write to "info:". Then you can use escapes shown at http://www.imagemagick.org/script/escape.php

For example, getting the average values as integers 0 to 255:

Code: Select all

magick ?.png -scale "1x1^!" -format "%f ( %[fx:floor(s.r*255+0.5)], %[fx:floor(s.g*255+0.5)], %[fx:floor(s.b*255+0.5)] )\n" info:

r.png ( 146, 89, 80 )
x.png ( 216, 210, 210 )
To loop through directories, use shell facilities, eg Windows "for". If using BAT scripts, double the percent signs.

Re: Looping through folders and output convert to txt file with filename

Posted: 2018-09-24T05:02:38-07:00
by RobShawUK
Thanks. I'm using 7.0.8-Q8 on Windows. I'll try that code

Re: Looping through folders and output convert to txt file with filename

Posted: 2018-09-26T02:31:13-07:00
by RobShawUK
This code works great thanks. Can a similar command be used to get the values for all pixels if the -scale is larger than 1x1? E.g.

magick 322_89.png -scale "4x4^!" test.txt

puts the 16 pixel colours in a text file but without the filename etc.

I'd want something like
r.png (0,0) (128,190,20)
r.png (0,1) (148,180,20)
etc

Thanks
Rob