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

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
Arko
Posts: 4
Joined: 2018-10-15T03:19:33-07:00
Authentication code: 1152

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

Post 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
Last edited by Arko on 2018-10-15T11:32:31-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detecting blank image files

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

Re: Detecting blank image files

Post 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?
snibgo's IM pages: im.snibgo.com
Arko
Posts: 4
Joined: 2018-10-15T03:19:33-07:00
Authentication code: 1152

Re: Detecting blank (empty, transparent) image files

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detecting blank image files

Post 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.
Arko
Posts: 4
Joined: 2018-10-15T03:19:33-07:00
Authentication code: 1152

Re: Detecting blank image files

Post 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 !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
Arko
Posts: 4
Joined: 2018-10-15T03:19:33-07:00
Authentication code: 1152

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

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