Page 1 of 1

Detecting blank (no pixel) image files - version 7.0.8 Q16

Posted: 2018-10-15T04:18:19-07:00
by Arko
Hi everyone,

I've seen several topics about approching subject. But I can't find the solution by myself.
I'm very new to shell and coding in general.

My goal is to detect blank image files in a given folder and delete those files.
Here's what I have so far (found in the forum) :

Code: Select all

 for file in D:/00_TAKROG/ATLAS_UNITS_AND_DOODADS_TEMPORARY/*.png; do
      	convert "$file" -threshold X% -format "%[fx:mean==1?1:0]" info:
    done
This seems to return my blank image files (is it correct ?). The topic where I found this piece of code said that this is suppose to find white images.

Code: Select all

 for file in D:/00_TAKROG/ATLAS_UNITS_AND_DOODADS_TEMPORARY/*.png; do
      	convert "$file" -threshold X% -format "%[fx:mean==1?1:0]" info:
			if [ ??? = "0" ]; then
				echo "$file seems to be blank - removing it..."
				rm "$file"
			fi
    done
Say I found how to detect my blank images, I don't know how to use the returned value found in the convert function.
Any help is welcome !

EDIT
I also found this solution :

Code: Select all

for file in D:/00_TAKROG/ATLAS_UNITS_AND_DOODADS_TEMPORARY/*.png; do
      histogram=`convert "$file" -threshold 50% -format %c histogram:info:-`
      white=`echo "${histogram}" | grep "white" | sed -n 's/^ *\(.*\):.*$/\1/p'`
      black=`echo "${histogram}" | grep "black" | sed -n 's/^ *\(.*\):.*$/\1/p'`
      blank=`echo "scale=4; ${black}/${white} < 0.005" | bc`
      if [ "${blank}" == "1" ]; then	
        echo "$file seems to be blank - removing it..."
        rm "$file"
      fi
    done
I do get the logic, but the langage is too cryptic for me. Here it gives me this error:

Code: Select all

bash: bc: command not found
I "understand" what it means, but I don't know how to correct it.

Thanks

Arko

Re: Detecting blank image files

Posted: 2018-10-15T10:16:10-07:00
by fmw42
You seem to be running a Unix shell on Windows. Perhaps Windows 10 unix or Cygwin. The bc command is a unix basic calculator, which seems to be missing from your unix environment. Often when you install unix on Windows there is a window that allow you to install bc. Otherwise, you can search Google for it and install separately.

Sorry, I am not a Widows user and know little more about that.

Please always provide your Imagemagick version and Platform/OS when asking questions.

Re: Detecting blank image files

Posted: 2018-10-15T10:25:30-07:00
by snibgo
Arko wrote:My goal is to detect blank image files in a given folder and delete those files.
What do you mean by "blank"? Entirely white? Entirely black? Or what?

Re: Detecting blank (empty, transparent) image files

Posted: 2018-10-15T10:42:46-07:00
by Arko
I'm using version 6.4.7. Q16.
By blank, I mean fully transparent, with no visible pixel.
fmw42 wrote:
2018-10-15T10:16:10-07:00
You seem to be running a Unix shell on Windows. Perhaps Windows 10 unix or Cygwin. The bc command is a unix basic calculator, which seems to be missing from your unix environment. Often when you install unix on Windows there is a window that allow you to install bc. Otherwise, you can search Google for it and install separately.

Sorry, I am not a Widows user and know little more about that.

Please always provide your Imagemagick version and Platform/OS when asking questions.
I'm exploring this track. I'll see where it leads.

Re: Detecting blank image files

Posted: 2018-10-15T10:48:07-07:00
by fmw42
IM 6.4.7 is so ancient that you should upgrade. It is well over 500 versions old. Did you mean 6.9.4.7 perhaps, which is still rather old.

For transparent images, you should extract the alpha channel and test if it is full black

Code: Select all

convert image -alpha extract -format "%[fx:mean==0?0:1]" info:
if return value is 0, then it is pure black and the image is fully transparent. Otherwise it returns 1 and the image is not fully transparent.

Re: Detecting blank image files

Posted: 2018-10-15T11:23:03-07:00
by Arko
fmw42 wrote:
2018-10-15T10:48:07-07:00
IM 6.4.7 is so ancient that you should upgrade. It is well over 500 versions old. Did you mean 6.9.4.7 perhaps, which is still rather old.

For transparent images, you should extract the alpha channel and test if it is full black

Code: Select all

convert image -alpha extract -format "%[fx:mean==0?0:1]" info:
if return value is 0, then it is pure black and the image is fully transparent. Otherwise it returns 1 and the image is not fully transparent.
You're right, the version I have installed is the 7.0.8 Q16.
Your code does return the right number. But I don't know how to include this returned number inside an if statement.

Code: Select all

for file in D:/00_TAKROG/ATLAS_UNITS_AND_DOODADS_TEMPORARY/*.png; do
    convert "$file" -alpha extract -format "%[fx:mean==0?0:1]" info:
	if [ ??? = "0" ]; then
		rm "$file"
	fi
done
I don't know what to put where the ??? are.

Thank you all for your time, you guys are very nice !

Re: Detecting blank (no pixel) image files - version 7.0.8 Q16

Posted: 2018-10-15T11:44:21-07:00
by fmw42
On Unix, you put the value into a variable and test on the variable.

Code: Select all

for file in D:/00_TAKROG/ATLAS_UNITS_AND_DOODADS_TEMPORARY/*.png; do
    test=$(magick "$file" -alpha extract -format "%[fx:mean==0?0:1]" info:)
	if [ "$test" = "0" ]; then
		rm "$file"
	fi
done
For IM 7, use magick rather than convert.

Re: Detecting blank (no pixel) image files - version 7.0.8 Q16

Posted: 2018-10-15T12:15:45-07:00
by Arko
Oh my goodness !!! THAT WORKS :lol:
Thank you so much !

Here's the final code for anyone like me chiming in.
To delete blank/empty images in a folder, do this :

Code: Select all

for file in D:/YOUR_FOLDER_HERE/YOUR_SUBFOLDER_HERE/*.png; do
    test=$(convert "$file" -alpha extract -format "%[fx:mean==0?0:1]" info:)
	if [ "$test" = "0" ]; then
		rm "$file"
	fi
done
Thanks again everyone, you are helping me and my team a lot !