Looping through folders and output convert to txt file with filename

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
RobShawUK
Posts: 7
Joined: 2018-09-24T02:14:53-07:00
Authentication code: 1152

Looping through folders and output convert to txt file with filename

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

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

Post 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.
snibgo's IM pages: im.snibgo.com
RobShawUK
Posts: 7
Joined: 2018-09-24T02:14:53-07:00
Authentication code: 1152

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

Post by RobShawUK »

Thanks. I'm using 7.0.8-Q8 on Windows. I'll try that code
RobShawUK
Posts: 7
Joined: 2018-09-24T02:14:53-07:00
Authentication code: 1152

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

Post 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
Post Reply