Page 1 of 1

Identify and delete images of one colour

Posted: 2010-08-03T05:17:18-07:00
by AlexMac
I have a folder of a few thousand images. They are all 8 bit PNGs, 400 x 400 pixels. I want to identify and delete all images of one solid colour R230 G246 B255. Is this possible using windows? Thanks

Re: Identify and delete images of one colour

Posted: 2010-08-03T09:54:36-07:00
by fmw42
I am not a windows user, but suspect you can batch script that.

I would find the min and max for each of r,g,b. If both were the same for each channel as your desired color, then delete the file. You can find all that information by using identify -verbose image

Re: Identify and delete images of one colour

Posted: 2010-08-03T20:25:00-07:00
by Drarakel
In your case (a check for a specific image with fixed dimensions - and a specific opacity?), you could also use "compare".
You could try that:

Code: Select all

@echo off
convert -depth 8 -size 400x400 xc:"rgb(230,246,255)" "%TEMP%\ch_test.mpc"
convert -depth 8 -size 400x400 xc:white "%TEMP%\ch_white.mpc"
SETLOCAL EnableDelayedExpansion
for %%i in (%1) do (
 set cmp=
 for /f %%t in ('compare -metric ae "%TEMP%\ch_test.mpc" "%%i" null: 2^>^&1') do set cmp=%%t
 if "!cmp!" == "0" (
  set cmp=
  for /f %%t in ('compare -metric ae "%TEMP%\ch_white.mpc" ^( "%%i" -alpha extract ^) null: 2^>^&1') do set cmp=%%t
  if "!cmp!" == "0" (
   del "%%i"
   echo Identical image "%%i" deleted.
  )
 )
)
del "%TEMP%\ch_test.mpc"
del "%TEMP%\ch_test.cache"
del "%TEMP%\ch_white.mpc"
del "%TEMP%\ch_white.cache"
set cmp=
Save that as batch file and use it with e.g. "check.cmd *.png" or "check *.*" or something like that.
This searches for 400x400 px images with your color and 100% opacity and deletes them.
But be careful - you should first try this in a different directory with a few copied files..

Re: Identify and delete images of one colour

Posted: 2010-08-04T06:32:07-07:00
by AlexMac
Thanks for the replies guys. I have created a batch file of the above code. Could you explain how I run this with regards to e.g. "check.cmd *.png" or "check *.*" . Bit of a novice at this. Many thanks.

Re: Identify and delete images of one colour

Posted: 2010-08-04T12:28:58-07:00
by fmw42
I think it might be faster to avoid compare and do something like the Windows Batch equivalent of the following unix script:


RR=230
GG=246
BB=255

LOOP OVER FILES

test1=`convert $infile -format "%[fx:standard_deviation.r==0&&standard_deviation.g==0&&standard_deviation.b==0)?1:0]" info:`
if [ $test1 -eq 1 ]; then
test2=`convert $infile -format "%[fx:(round(255*mean.r)==$RR&&round(255*mean.g)==$GG&&round(255*mean.b)==$BB)?1:0]" info:`
if [ $test2 -eq 1 ]; then
# delete image
else
# don't delete image
fi
else
# don't delete image
fi

END LOOP


NOTE:

An alternate to:


test1=`convert $infile -format "%[fx:standard_deviation.r==0&&standard_deviation.g==0&&standard_deviation.b==0)?1:0]" info:`
if [ $test1 -eq 1 ]; then

is


test1=`convert $infile -format "%[fx:abs(standard_deviation.r + standard_deviation.g + standard_deviation.g)]" info:`
if [ "$test1" = "0" ]; then

Not sure which would be faster, but probably negligible difference.


See viewtopic.php?f=3&t=16786 as one cannot use the global std via string format

Re: Identify and delete images of one colour

Posted: 2010-08-04T19:08:28-07:00
by Drarakel
AlexMac wrote:Could you explain how I run this
Store the batch file in the directory where your images are located, for example (or in a more general directory that is included in your PATH variable). Then open a command prompt. Go to the directory with your images. If your batch file is called 'batch123.cmd', for example, and you want to process all PNG files, then type "batch123 *.png" here.
(But as I said, you should try it first in a directory with a few copies to see if it's really doing what you're aiming for.)

One could change the script to a 'drag and drop'-variant, too - if you need it like that...
And one could use different methods within the script, e.g. Fred's (fmw42) methods. But the processing speed should be already sufficient in your case, I guess.