KAMIKAZE
KAMIKAZE

Reputation: 510

ImageMagick: detect fully transparent blank images

I'm using ImageMagick 6 for slicing my images into parts. How to split an image with a grid and preserve transparency bounding box

dim=`convert image.png -format "%wx%h" info:`
convert \( -size $dim xc:none \) null: \( image.png -crop 10x10% \) -layers composite result.png

All works perfect, but if I use input image like this:

enter image description here

On output I'm getting some fully transparent empty images, because from input image - there is nothing at the corners of it.

Yes, that how it should work, however I'm looking how to avoid that.

Or remove that blank images from disk, because I don't need them.

If there is no normal way to do this?

I'm thinking about workaround also, like detect fully empty images and modify them(fill with color or any other, no matter), so after that I can sort output files in Finder just by modified date and remove them manually.

p.s. I have images like that circle 2000 x 2000px size and slice at 5%, imagine number of empty images on output.. remove that manually is more than a pain.

Upvotes: 0

Views: 757

Answers (1)

fmw42
fmw42

Reputation: 53184

You need to write a script loop as follows in Unix Imagemagick 6. I have put your input image in a directory called test on my desktop. I am not sure why you used your commands above, but it can cropped directly without the layers composite. I save all the cropped images to layers of a temporary miff file, which I delete at the end. The basic idea is to test each cropped image to see if its alpha channel is pure black (or nearly pure black to avoid tiny very transparent single pixels).

1) If it is black within some small threshold, then remove the alpha channel from the miff layer saving it as black numbered output image. Otherwise, just save the layer as a numbered output image.

cd
cd desktop/test
convert image.png -background black -alpha background -crop 10x10% image.miff
num=`convert image.miff -format "%n\n" info: | head -n 1`
for ((i=0; i<num; i++)); do
trans_test=`convert image.miff[$i] -alpha extract -scale 1x1! -format "%[fx:mean<=0.0001?0:1]" info:`
echo "i=$i test=$trans_test"
if [ $trans_test -eq 0 ]; then
convert image.miff[$i] -alpha off result-$i.png
else
convert image.miff[$i] result-$i.png
fi
done
rm -f image.miff
cd


2) If the alpha channel is nearly black, skip that layer

cd
cd desktop/test
convert image.png -background black -alpha background -crop 10x10% image.miff
num=`convert image.miff -format "%n\n" info: | head -n 1`
for ((i=0; i<num; i++)); do
trans_test=`convert image.miff[$i] -alpha extract -scale 1x1! -format "%[fx:mean<=0.0001?0:1]" info:`
echo "i=$i test=$trans_test"
if [ $trans_test -eq 1 ]; then
convert image.miff[$i] result-$i.png
fi
done
rm -f image.miff
cd


3) If the alpha channel is black skip it, but number the results sequentially for only the ones kept.

cd
cd desktop/test
j=0
convert image.png -background black -alpha background -crop 10x10% image.miff
num=`convert image.miff -format "%n\n" info: | head -n 1`
for ((i=0; i<num; i++)); do
trans_test=`convert image.miff[$i] -alpha extract -scale 1x1! -format "%[fx:mean<=0.0001?0:1]" info:`
echo "i=$i test=$trans_test"
if [ $trans_test -eq 1 ]; then
convert image.miff[$i] result-$j.png
j=$((j+1))
fi
done
rm -f image.miff
cd


4) If the alpha channel is black skip it, but number the results sequentially for only the ones kept with leading zeros.

cd
cd desktop/test
j=0
convert image.png -background black -alpha background -crop 10x10% image.miff
num=`convert image.miff -format "%n\n" info: | head -n 1`
for ((i=0; i<num; i++)); do
trans_test=`convert image.miff[$i] -alpha extract -scale 1x1! -format "%[fx:mean<=0.0001?0:1]" info:`
echo "i=$i test=$trans_test"
if [ $trans_test -eq 1 ]; then
jj=`printf "%02d" $j`
convert image.miff[$i] result-$jj.png
j=$((j+1))
fi
done
rm -f image.miff
cd

Upvotes: 1

Related Questions