detect fully black images

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?".
Locked
andienz
Posts: 10
Joined: 2010-09-09T15:34:32-07:00
Authentication code: 8675308

detect fully black images

Post by andienz »

Hi!

I need to weed through thausands of pictures and get rid of the ones that only have black pixels.
What is the fastest way to detect images that contain nothing else but black. grb 0,0,0.
The Key word is fast.

I'm using ImageMagick 6.6.4-4 2010-09-17 Q8 with cygwin
Any suggestions?

Thanks!

Andi

lwhistler
Posts: 23
Joined: 2010-09-15T15:33:29-07:00
Authentication code: 8675308

Re: detect fully black images

Post by lwhistler »

Since all black images have a much smaller file size than images with detail you could use a bash script to delete all files below a certain amount of bytes. Check the properties of one of the black images for file size.

This will delete all jpg files smaller than 20k.

file.sh

Code: Select all

#!/bin/bash

for image_file in *.jpg
do
filesize=$(stat -c%s "$image_file")

if [ $filesize -lt 20000 ]; then
rm $image_file
fi
done

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: detect fully black images

Post by fmw42 »

mean=`convert image -format "%[mean]" info:`
if [ "$mean" = 0 ]; then
echo "totally black image"
else
echo "not totally black image"
fi


or if you want a threshold


thresh=XX (XX as fraction between 0 and 1)
mean=`convert image -format "%[mean]" info:`
meantest=`convert xc: -format "%[fx:($mean/quantumrange)<$thresh?1:0]" info:`
if [ $meantest -eq 1 ]; then
echo "essentially black image"
else
echo "not black image"
fi


see string formats: http://www.imagemagick.org/script/escape.php

and fx escapes: http://www.imagemagick.org/Usage/transform/#fx_escapes

and fx at: http://www.imagemagick.org/script/fx.php

Locked