# annoter_images_taille_dyn.sh

# source : http://gangand.net/ii/image

# annoter des images en fonction de la taille de chaque image

# https://stackoverflow.com/questions/58621445/imagemagick-how-to-create-an-annotation-that-would-fit-its-size-into-any-image?newreg=53997a2d9b724f1fb5c70113ea3c1332


# exemple syntaxe fonctionnel :
# convert bricelets_20260425_082255.jpg -set option:size %[w]x -fill white -background "#00000080" \( label:"Bricelets suisses" -virtual-pixel background -distort SRT "0.8 0" -virtual-pixel none -distort SRT "0.8 0" \) -gravity south -composite compo/bricelets_20260425_082255.jpg
# IMPORTANT : \( et \), bacjkground avec guillemets

# ==========================================================
# Using ImageMagick version 6 or 7 you can make a label sized to fit any input image, with a semi-transparent background, and composite it at the bottom of the input image to get the result you describe. Here is a command with IM 6 that does it...
# 
# convert input.png -set option:size %[w]x \
#    -fill white -background "#00000080" \
#    \( label:"This is my text." \
#       -virtual-pixel background -distort SRT "0.8 0" \
#       -virtual-pixel none -distort SRT "0.8 0" \) \
#    -gravity south -composite result.png
# That uses the width of the input image "%[w]" to set the width of the label. It sets the text color to white and the background to semi-transparent black, "#00000080".
# 
# Inside the parentheses it creates your label. It uses "distort SRT" to scale the label down a bit to pull it in from the edges. Then it scales the label down a bit more to add some transparent space around it.
# 
# After the label is created it sets the gravity to "south" and composites the label onto the input image. It finishes by writing the output file.
# 
# Using IM 7 you'll need to change "convert" to "magick". For Windows change the continued line backslashes "\" to carets "^" and get rid of the backslashes that escape the parentheses.
# 
# Edited to add: Normally you'd use "-size WxH" ahead of making a "label:" to constrain it within particular dimensions. I used "-set option:size" instead because it allows for using percent escapes like "%[w]" with IM 6. That way the label dimensions are relative to any input image width.
# ==========================================================

function syntaxe () {
   echo "Syntaxe :"
   echo $(basename $0) go  : annoter les images avec le nom du fichier
   echo $(basename $0) echo  : créer un fichier shell modifiable
}

if [ "x$1" = "x" ]; then
   syntaxe
   exit
else
   if [ "x$1" = "xecho" ]; then
      echo Mode script perso
   elif [ "x$1" = "xgo" ]; then
      echo Mode auto
   else
      syntaxe
      exit
   fi
fi

listFiles=/tmp/annoter_images_taille_dyn.txt
ls -1 *jpg | sort > $listFiles
workdir=/tmp/annoter_images_taille_dyn_$(date "+%A_%e_%h_%Y-%Hh%Mmn%Ss")
mkdir $workdir

# 1. copier les images
echo $(date) Pour afficher seulement les commandes prévues, ajouter echo en 1er paramètre
echo $(date) 1. copie jpg
while read jpg; do
    cp $jpg $workdir
done < $listFiles

# 2. annoter les images
echo $(date) 2. annotation
cd $workdir

# annotation nom fichier complet, extension comprise
# ls -1 *.jpg | xargs -i convert {} -set option:size %[w]x \
#   -fill white -background "#00000080" \
#   \( label:"{}" \
#      -virtual-pixel background -distort SRT "0.8 0" \
#      -virtual-pixel none -distort SRT "0.8 0" \) \
#   -gravity south -composite {}


# cp $listFiles $workdir
if [ "x$1" = "xecho" ]; then
   bash_perso=annoter_personnalisé.sh
   echo "# fichier $workdir/$bash_perso" > $bash_perso
   echo "# généré par $0" >> $bash_perso
   echo "" >> $bash_perso

   dirResultA=annoter
   echo "mkdir $dirResultA" >> $bash_perso
   echo \# ============================== >> $bash_perso
   echo \# taille proportionnelle aux dimensions >> $bash_perso
   echo \# 'légende = nom de fichier' >> $bash_perso
   echo \# ============================== >> $bash_perso
   while read jpg; do
       f=$(echo "$jpg" | sed 's/.jpg//')
       
       echo convert $jpg -set option:size %[w]x \
      -fill white -background \"#00000080\" \
      \\\( label:\""$f"\" \
         -virtual-pixel background -distort SRT \"0.8 0\" \
         -virtual-pixel none -distort SRT \"0.8 0\" \\\) \
      -gravity south -composite $dirResultA/$jpg
   done < $listFiles >> $bash_perso
   
   echo >> $bash_perso
   echo \# ============================== >> $bash_perso
   echo \# forcer la taille de la legende >> $bash_perso
   echo \# ============================== >> $bash_perso
   echo >> $bash_perso
   dirResultB=annoterLabelFixe
   echo "mkdir $dirResultB" >> $bash_perso
   while read jpg; do
       f=$(echo "$jpg" | sed 's/.jpg//')
       echo 'taillePoint=170'
       echo convert $jpg -pointsize \$taillePoint \
      -fill white -background \"#00000080\" \
      \\\( label:\""$f"\" \
         -virtual-pixel background -distort SRT \"0.8 0\" \
         -virtual-pixel none -distort SRT \"0.8 0\" \\\) \
      -gravity south -composite $dirResultB/$jpg
   done < $listFiles >> $bash_perso

   echo >> $bash_perso
   echo \# ====================================== >> $bash_perso
   echo \# label identique pour toutes les images >> $bash_perso
   echo \# ====================================== >> $bash_perso
   echo >> $bash_perso
   dirResultC=annoterTailleFixe
   echo "mkdir $dirResultC" >> $bash_perso
   echo \# labelIdem=\"Votre label identique ici\" >> $bash_perso
   while read jpg; do
       f=$(echo "$jpg" | sed 's/.jpg//')       
       echo convert $jpg -pointsize \$taillePoint \
      -fill white -background \"#00000080\" \
      \\\( label:\""$labelIdem"\" \
         -virtual-pixel background -distort SRT \"0.8 0\" \
         -virtual-pixel none -distort SRT \"0.8 0\" \\\) \
      -gravity south -composite $dirResultC/$jpg
   done < $listFiles >> $bash_perso

   chmod +x $bash_perso
   geany $bash_perso &
   echo "==============================================================="
   echo "===== Fichier à lancer : $bash_perso ====="
   echo "==============================================================="
   exit
   
else
   while read jpg; do
   echo "$(date)    annotation $jpg"

       f=$(echo "$jpg" | sed 's/.jpg//')
       convert $jpg -set option:size %[w]x \
      -fill white -background "#00000080" \
      \( label:"$f" \
         -virtual-pixel background -distort SRT "0.8 0" \
         -virtual-pixel none -distort SRT "0.8 0" \) \
      -gravity south -composite $jpg
   done < $listFiles
fi


# 3. résultat
echo $(date) 3. fichiers créés dans $workdir
nemo $workdir &