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.