Advertisement
GegoXaren

process_text_to_image.sh

Aug 25th, 2019 (edited)
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.11 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # ------
  4. # Changes
  5. #
  6. # 2018-09-03:
  7. #   * added --no-header for when you want to use your own pandoc header
  8. #
  9. # 2018-09-22
  10. #   * Fixed --no-header...
  11. #     Seemed to have forgotten the "$" infront of the variable.
  12. #
  13. # 2021-01-13
  14. #   * fixed up the if statments.
  15. #
  16. # ------
  17.  
  18. ARGS=()
  19. ARGS="${@}"
  20.  
  21. DPI=300
  22.  
  23. IN_FILE=
  24. OUT_FILE=big_image
  25. PERSERVE_TMP=false
  26. INVERT_COLOURS=false
  27. _NO_PANDOC_HEADER=false
  28.  
  29. CWD=$PWD
  30.  
  31. _PANDOC_HEADER="
  32. geometry: margin=0.5cm
  33. papersize: a5
  34. mainfont: DejaVu Serif
  35. fontsize: 12pt
  36. "
  37.  
  38. TITLE=""
  39.  
  40. echo "-----"
  41. echo "---"
  42. echo "--"
  43. echo "-"
  44.  
  45. print_help() {
  46.  
  47.   echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
  48.   echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
  49.   echo ""
  50.   echo "!IMPORTANT! The folder \"./tmp/\" in the current working directory will be"
  51.   echo "            used as a temporary storage, and may be deleted, along with it's"
  52.   echo "            contents!"
  53.   echo ""
  54.   echo "---------------------"
  55.   echo ""
  56.   echo "-h  --help"
  57.   echo "    Print this help message"
  58.   echo ""
  59.   echo "-i <file>   --input <file>"
  60.   echo "    The file to use to convert to an image. "
  61.   echo ""
  62.   echo "-o <file>   --output <file>"
  63.   echo "    The image to output to. (Default=big_image.png)"
  64.   echo ""
  65.   echo "-d <integer>    --dpi <integer>"
  66.   echo "    Set the dpi of the intermediate image relative to an a5 paper."
  67.   echo "    (Default=300)"
  68.   echo ""
  69.   echo "-p      --perserve"
  70.   echo "    Do not delete the TMP folder."
  71.   echo ""
  72.   echo "-invert"
  73.   echo "    Invert the colours of the final image."
  74.   echo ""
  75.   echo "-t \"name\" --title \"name\""
  76.   echo "    Set the title on the the title page."
  77.   echo ""
  78.   echo "--no-header"
  79.   echo "    Do not insert the pandoc header. (Default:"
  80.   echo "$_PANDOC_HEADER"
  81.   echo ")"
  82.   echo ""
  83.   echo "---------------------"
  84.   echo ""
  85.   echo "If you are getting an error from convert that the height or width exeeds"
  86.   echo "some value, you may want to check the ImageMagick policy.xml file."
  87.   echo ""
  88.   echo "The path to ImageMagick policy file is:"
  89.   convert -list policy | grep .xml
  90.   echo ""
  91.   echo "---------------------"
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. main() {
  99.   #ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
  100.  
  101.   echo "IN_FILE\: $IN_FILE"
  102.   echo "OUT_FILE\: $OUT_FILE"
  103.   echo "CWD\: $CWD"
  104.   echo "DPI: $DPI"
  105.   #echo "ESCAPED_CWD\: $ESCAPED_CWD"
  106.  
  107.   if [[ ! -e "$CWD/$IN_FILE" ]]
  108.   then
  109.     echo "!!!in file does not exist!!!"
  110.     echo ""
  111.     #print_help
  112.     exit 1
  113.   fi
  114.  
  115.   # first we create a temp folder.
  116.   mkdir -p "$CWD/tmp"
  117.  
  118.   #next we want to copy our file into it.
  119.   cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
  120.   cd "$CWD/tmp"
  121.  
  122.   # Now we can start the work for this.
  123.   if [[ $_NO_PANDOC_HEADER == false ]]
  124.   then
  125.     # We add a special header to the file to make it pandoc know what to do.
  126.    
  127.     printf '%s\n' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
  128.     if [ -n TITLE="" ]
  129.     then
  130.       printf '%s\n' "title: ${TITLE}" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
  131.     fi
  132.    
  133.     printf '%s' "$_PANDOC_HEADER" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
  134.    
  135.     printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
  136.   fi
  137.  
  138.   # Now we use pandoc to do to convert it to a PDF.
  139.   pandoc --pdf-engine=xelatex "$CWD/tmp/text.txt" -o "$CWD/tmp/text.pdf"
  140.  
  141.   pdfcrop --margins '10 5 10 5' "$CWD/tmp/text.pdf" "$CWD/tmp/text-croped.pdf"
  142.  
  143.   # Convert it to images
  144.   pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
  145.  
  146.   # convert make the colour space greyscale and the append to each other
  147.   convert -append -colorspace gray +matte -depth 8 "$CWD/tmp/page-*.png" "$CWD/tmp/big-page.png"
  148.  
  149.   FINAL_IMAGE=""
  150.  
  151.   # If we invert the final image this is where we do it.
  152.   if [[ $INVERT_COLOURS == true ]]
  153.   then
  154.     convert "$CWD/tmp/big-page.png" -channel RGB -negate "$CWD/tmp/big-page-inverted.png"
  155.     FINAL_IMAGE="$CWD/tmp/big-page-inverted.png"
  156.   else
  157.     FINAL_IMAGE="$CWD/tmp/big-page.png"
  158.   fi
  159.  
  160.   cp "$FINAL_IMAGE" "$CWD/$OUT_FILE.png"
  161. }
  162.  
  163.  
  164. parse_args() {
  165.   if [[ -z "$1" ]]
  166.   then
  167.     echo "Try --help or -h."
  168.     exit 1
  169.   fi
  170.  
  171.  
  172.   while [[ $# -gt 0 ]]
  173.   do
  174.    
  175.     case "${1}" in
  176.       -i|--input)
  177.       IN_FILE="$2"
  178.       shift
  179.       shift
  180.       ;;
  181.       -o|--output)
  182.       OUT_FILE="$2"
  183.       shift
  184.       shift
  185.       ;;
  186.       -t|--title)
  187.       TITLE="$2"
  188.       shift
  189.       shift
  190.       ;;
  191.       -d|--dpi)
  192.       DPI="$2"
  193.       shift
  194.       shift
  195.       ;;
  196.       -p|--perserve)
  197.       PERSERVE_TMP=true
  198.       shift
  199.       ;;
  200.       --invert)
  201.       INVERT_COLOURS=true
  202.       shift
  203.       ;;
  204.       --no-header)
  205.       _NO_PANDOC_HEADER=true
  206.       shift
  207.       ;;
  208.       -h|--help)
  209.       print_help
  210.       exit
  211.       shift
  212.       ;;
  213.       *)
  214.       #print_help
  215.       #exit 1
  216.       shift
  217.       ;;
  218.       --)
  219.       shift
  220.       break
  221.       ;;
  222.     esac
  223.   done
  224. }
  225.  
  226. parse_args "${@}"
  227. main
  228.  
  229. if [[ $PERSERVE_TMP = true ]]
  230. then
  231.   echo "Not cleaning up!"
  232. else
  233.   rm -r "$CWD/tmp"
  234. fi
  235.  
  236. echo "-"
  237. echo "--"
  238. echo "---"
  239. echo "----"
  240.  
  241.  
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement