2

Someone wanted to sort files in a directory according to whether the files are pictures(camera) vs. design graphics. On Stackoverflow, there is an older solution/proof-of-concept involving the comparing of the color range in images. I reproduce it almost as-is:1

CheckCartoon.sh:

#!/bin/sh

IMAGE=$1
COLORS=$(convert $IMAGE -format %c histogram:info:- | wc -l)
WIDTH=$(identify $IMAGE | sed -r "s/.* ([0-9]+)x[0-9]+ .*/\1/")
HEIGHT=$(identify $IMAGE | sed -r 's/.* [0-9]+x([0-9]+) .*/\1/')
RATIO=$(echo $WIDTH $HEIGHT $COLORS | awk '{ print ($1 * $2)/$3;}')
echo $RATIO | awk '{ printf "%020.5f",$1 }' 

CheckAll.sh:

#!/bin/sh

FILES=$(ls)
for FILE in $FILES; do
IsIMAGE=$(identify $FILE 2>&1 | grep " no decode delegate " | grep -o "no")
if [ "$IsIMAGE" = "no" ]; then continue; fi
IsIMAGE=$(identify $FILE 2>&1 | grep " Improper image header " | grep -o "Improper")
if [ "$IsIMAGE" = "Improper" ]; then continue; fi
echo $(CheckCartoon.sh $FILE) $FILE
done

Typical output:

00000000000002.16959 Burne_Jones_The_Doom_Fulfilled_1884_85.jpg
00000000047480.70000 Dürer-Hieronymus-im-Gehäus.jpg
00000000000016.03440 IMG_00000138.jpg
00000000000015.67780 IMG_00000143.jpg
00000000000014.37140 IMG_00000144.jpg
00000000000015.05500 IMG_00000145.jpg
00000000000011.79040 M2-GrandTurk1.jpg
00000000000013.05840 Real-01.jpg

I think the solution is interesting despite its shortcomings and I'd like to explore it and:

  • use best practices to update it and make it safer, and more robust as I think the scripting was just added to showcase the idea
  • have it create two directories to copy the files according to a threshold that would be set in the script or as a parameter at runtime. Generally speaking, very low value is supposed to mean something akin to a camera picture(or at least not a color range limited sort of graphic). So here, had I set the threshold to 50, all but one images would have been copied to the pictures directory, whereas the Durer one would go to graphics.

I would like some advice on trying to do that.


1. The answer on SO suffers from the editor doing something wrong with the backticks. Furthermore, there is bold? markup inside one of the blocks. Surely everyone trying figures it out but a beginner probably won't.

  • Follow up Q here. – user44370 Nov 16 '14 at 7:07
1

If you use mkdir -p there won't be any complaints about directories already existing, so put the following at the top of CheckAll.sh:

 mkdir -p graphics pictures

The line echo $(CheckCartoon.sh $FILE) $FILE just displays the result of the calculation together with the filename, you should do something more with it, so replace it with:

# store value
result=$(CheckCartoon.sh $FILE)
# do a string compare
if [[ $result < "00000000000050." ]]
then
    echo move "$FILE" to graphics $result
    mv "$FILE" graphics
else
    echo move "$FILE" to pictures $result
    mv "$FILE" pictures
fi

As for robustness, you should probably not be using shell scripting in the first place. But you could at least put additional checks on $result to make sure the script returned a valid value.

  • Thank you, that's helpful! The threshold as an if statement. I think I'll cp instead of moving and it's actually > (supposedly it's the pics that have low value). It's not clear ultimately the sort is significant; maybe it can help assist in identification. Time will tell, thanks again! – user44370 Nov 15 '14 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy