gen_tiles.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. trap clean_tmp EXIT
  3. # fin d'eviter tout problème de locales en reste en C de base.
  4. set -e
  5. export LANG=C
  6. # pour éliminer systématiquement les fichier temporaires créés ic
  7. function clean_tmp() {
  8. if [ -n "$wfname" ]; then
  9. rm $wfname
  10. fi
  11. if [ -n "$tmp_file" ]; then
  12. rm $tmp_file
  13. fi
  14. }
  15. test_mode=false
  16. memory_limit=256
  17. crop_x=256
  18. crop_y=256
  19. min_scale=0
  20. max_scale=8
  21. usage="$0 [-x <x_tile_size>] [-y <y_tile_size>] [-p <prefix_result>] [-t] [-h] [-m <min_zoom>] [-M <max_zoom>] <image_to_convert>\n example: $0 -r test_res"
  22. if ! which anytopnm pnmscale convert > /dev/null; then
  23. echo "il faut installer les paquets netpbm et imageMagick pour utiliser ce script !"
  24. fi
  25. while getopts m:M:x:y:p:ht prs
  26. do
  27. case $prs in
  28. t) test_mode=true;;
  29. x) crop_x=$OPTARG;;
  30. y) crop_y=$OPTARG;;
  31. m) min_scale=$OPTARG;;
  32. M) max_scale=$OPTARG;;
  33. p) prefix=$OPTARG;;
  34. \? | h) echo -e $usage
  35. exit 2;;
  36. esac
  37. done
  38. shift `expr $OPTIND - 1`
  39. if [ -z "$1" ]; then
  40. echo -e "usage :\n$usage"
  41. exit 1
  42. elif [ ! -f "$1" ]; then
  43. echo -e "le paramètre $1 ne correspond pas à un nom de fichier !"
  44. exit 1
  45. fi
  46. fname=$1
  47. dir=$(dirname $fname)
  48. if [ -z "$prefix" ]; then
  49. prefix=$(basename $1|sed 's/\..*$//')
  50. fi
  51. wfname=$(mktemp ${prefix}_XXXX.pnm)
  52. if ! $test_mode; then
  53. anytopnm $fname > $wfname
  54. else
  55. echo "anytopnm $fname > $wfname"
  56. fi
  57. echo "préfixe : "$prefix
  58. tmp_file=$(mktemp)
  59. for ((z=$min_scale; z <= $max_scale; z++))
  60. do
  61. fprefix=${prefix}00$z
  62. printf -v ratio %1.4lf $(echo "1 / (2^$z)" | bc -l)
  63. echo génération du ratio $ratio
  64. zwfname=$tmp_file
  65. if $test_mode; then
  66. if [ $ratio = 1.0000 ]; then
  67. zwfname=$wfname
  68. else
  69. echo "pnmscale $ratio $wfname > $zwfname"
  70. fi
  71. echo convert $zwfname \
  72. -limit memory $memory_limit \
  73. -crop ${crop_x}x${crop_x} \
  74. -set filename:tile "%[fx:page.x/${crop_x}]_%[fx:page.y/${crop_y}]" \
  75. +repage +adjoin "${fprefix}_%[filename:tile].jpg"
  76. else
  77. if [ $ratio = 1.0000 ]; then
  78. zwfname=$wfname
  79. else
  80. if ! pnmscale $ratio $wfname > $zwfname; then
  81. echo "operation 'pnmscale $ratio $wfname > $zwfname' en erreur"
  82. exit 1
  83. fi
  84. fi
  85. if convert $zwfname \
  86. -limit memory $memory_limit \
  87. -crop ${crop_x}x${crop_x} \
  88. -set filename:tile "%[fx:page.x/${crop_x}]_%[fx:page.y/${crop_y}]" \
  89. +repage +adjoin "${fprefix}_%[filename:tile].jpg"; then
  90. echo "Nombre des fichiers produits :" $(ls -la ${fprefix}_*| wc -l)
  91. else
  92. echo "operation 'convert' en erreur"
  93. exit 2
  94. fi
  95. fi
  96. done
  97. if ! $test_mode; then
  98. ## les lignes ci dessous sont destinnées à mettre des 0 en debut des numéros de ligne et de colonnes
  99. ## Il y a certainement plus simple mais là c'est du rapide et efficace.
  100. rename 's/_(\d\d)_(\d+\.jpg)$/_0$1_$2/' ${prefix}*
  101. rename 's/_(\d)_(\d+\.jpg)$/_00$1_$2/' ${prefix}*
  102. rename 's/_(\d+)_(\d\d)(\.jpg)$/_$1_0$2$3/' ${prefix}*
  103. rename 's/_(\d+)_(\d)(\.jpg)$/_$1_00$2$3/' ${prefix}*
  104. fi