Advertisement
howtophil

togreytext.sh

Feb 20th, 2024 (edited)
969
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.63 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #------------------------------------------------
  4. # A way to send images over thin text-based
  5. # mediums.
  6. #
  7. # This one is "togreytext.sh"
  8. # get "fromgreytext.sh" here: https://pastebin.com/eSGZ8ZVD
  9. #
  10. # Following the concept of SSTV (Slow Scan TV),
  11. # that images do not need to be perfect or large
  12. # when transmitted over thin mediums, I have made
  13. # This pair of scripts.
  14. #
  15. # The first one, "togreytext.sh" converts an image
  16. # into a small 26-grays grayscale image composed
  17. # of a substitution of letters for numbers in a PGM
  18. # uncompressed image file.
  19. #
  20. # ./togreyutext.sh "infile.jpg" 128
  21. #
  22. # This "compressed" PGM can then be sent over LoRa
  23. # or text transmission methods.
  24. #
  25. # Once on the other side, "fromgreytext.sh" can
  26. # be run on the resulting file. It will expand
  27. # the letter back into numbers and set the
  28. # header correctly again.
  29. #
  30. # ./fromgreytext.sh captured.txt>outfile.pgm
  31. #
  32. # That file is then a valid uncompressed PGM
  33. # though grayscale and smaller than the original.
  34. # That PGM can then be converted to your image
  35. # format of choice.
  36. #
  37. # ~HowToPhil 2024/02/21 00:04 EST
  38. #------------------------------------------------
  39.  
  40. # Put the args into pretty vars
  41. FILE="$1" #The image file to convert for transmission
  42. SQUARE="$2" #The squared geometry of the sent image
  43.  
  44. #Preserve the pgm header but convert the line-returns to spaces for compactness
  45. convert "$FILE" -scale $SQUAREx$SQUARE -compress none pgm: |head -n3| tr "\n" " "|sed 's/^[ \t]*//;s/[ \t]*$//';echo
  46.  
  47. # Convert the image to a scaled-down version.
  48. # Make the image greyscale.
  49. # This will be in uncompressed PGM format.
  50. # Clamp grey values into 26 groups which will
  51. # be represented by upper-case letters.
  52. # Also remove all the spaces after the number-to-letters
  53. # but is done.
  54. # This essentially compresses and compacts the image
  55. # for transmission as text.
  56.  
  57. convert "$FILE" -scale $SQUAREx$SQUARE -compress none pgm: |sed -e 1,3d\
  58. |sed "s/\b[0-9]\b/A/g" \
  59. |sed "s/\b1[0-9]\b/B/g"\
  60. |sed "s/\b2[0-9]\b/C/g"\
  61. |sed "s/\b3[0-9]\b/D/g"\
  62. |sed "s/\b4[0-9]\b/E/g"\
  63. |sed "s/\b5[0-9]\b/F/g"\
  64. |sed "s/\b6[0-9]\b/G/g"\
  65. |sed "s/\b7[0-9]\b/H/g"\
  66. |sed "s/\b8[0-9]\b/I/g"\
  67. |sed "s/\b9[0-9]\b/J/g"\
  68. |sed "s/\b10[0-9]\b/K/g"\
  69. |sed "s/\b11[0-9]\b/L/g"\
  70. |sed "s/\b12[0-9]\b/M/g"\
  71. |sed "s/\b13[0-9]\b/N/g"\
  72. |sed "s/\b14[0-9]\b/O/g"\
  73. |sed "s/\b15[0-9]\b/P/g"\
  74. |sed "s/\b16[0-9]\b/Q/g"\
  75. |sed "s/\b17[0-9]\b/R/g"\
  76. |sed "s/\b18[0-9]\b/S/g"\
  77. |sed "s/\b19[0-9]\b/T/g"\
  78. |sed "s/\b20[0-9]\b/U/g"\
  79. |sed "s/\b21[0-9]\b/V/g"\
  80. |sed "s/\b22[0-9]\b/W/g"\
  81. |sed "s/\b23[0-9]\b/X/g"\
  82. |sed "s/\b24[0-9]\b/Y/g"\
  83. |sed "s/\b25[0-9]\b/Z/g"\
  84. |sed 's/[[:space:]]//g'
  85.  
  86.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement