Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.66 KB | None | 0 0
  1. #!/bin/bash
  2. #Check if imagemagick is installed and exit if missing
  3.  
  4. command -v convert >/dev/null 2>&1 || { echo >&2 "I require ImageMagick but it's not installed.  Aborting."; exit 1; }
  5.  
  6. #Get CLI args
  7.  
  8. OPTIND=1
  9.  
  10. top_text="TOP TEXT"
  11. bottom_text="BOTTOM TEXT"
  12. image_file="/home/anon/Pictures/pepe/thumbsup.jpg"
  13.  
  14. while getopts "h?t:b:f:" opt; do
  15.  
  16.     case "$opt" in
  17.     h|\?)
  18.         printf "Available options are:\n t - Top Text\n b - Bottom Text\n f - Path to image file - Mandatory!\n"
  19.         exit 0
  20.         ;;
  21.     t)  top_text="$OPTARG"
  22.         ;;
  23.     b)  bottom_text="$OPTARG"
  24.         ;;
  25.     f)  image_file="$OPTARG"
  26.         ;;
  27.     esac
  28. done
  29.  
  30. shift $((OPTIND-1))
  31.  
  32. [ "$1" = "--" ] && shift
  33.  
  34. #Make sure an image file was set and that it exists, or, what are we doing here, really. (Does not successfully check when source is a URL that fails to load, https for example)
  35. if [ -z "$image_file" ]
  36.     then echo "You must specify an image file. Use -h or -? for help."; exit 1
  37. fi
  38.  
  39. #Let the user know any options that were invalid that were not allready    handled by getopts.
  40. if [ ! -z "$@" ]
  41.     then printf "Invalid arguement(s): \"$@\"\nIgnored, continuing...\n"
  42. fi
  43.  
  44. #Get some info on the file path etc
  45. path=${image_file%/*}
  46. base=${image_file##*/}
  47. fext=${base##*.}
  48. pref=${base%.*}
  49.  
  50. #Upscale the image if it's too shitty. This is required to make the text     readable as it's generated to fit the pixels of the original
  51. convert -resize 1024x1024\< "$image_file" shitpost-temp-resize.gif
  52.  
  53. #Get the height and width of the image to use later to size the overlays. Clipped height keeps things locked in the top and bottom fifth of the image.
  54. image_width=`identify -format "%[width]" shitpost-temp-resize.gif`
  55. image_height=`identify -format "%[height]" shitpost-temp-resize.gif`
  56. clipped_height=$(($image_height/5))
  57. stroke_width=$(($clipped_height/40))
  58.  
  59. #Finishing up
  60. convert -background none -fill white -stroke black -strokewidth "$stroke_width" -size "$image_width"x"$clipped_height" -gravity Center -font 'Impact' caption:"$top_text" temp-top-text.gif
  61. convert -background none -fill white -stroke black -strokewidth "$stroke_width" -size "$image_width"x"$clipped_height" -gravity Center -font 'Impact' caption:"$bottom_text" temp-bottom-text.gif
  62. composite -gravity north temp-top-text.gif shitpost-temp-resize.gif shitpost-temp-composite.gif
  63. composite -gravity south temp-bottom-text.gif shitpost-temp-composite.gif shitpost-"$pref"."$fext"
  64.  
  65. #Clean up
  66. rm temp-top-text.gif
  67. rm temp-bottom-text.gif
  68. rm shitpost-temp-resize.gif
  69. rm shitpost-temp-composite.gif
  70.  
  71. #Announce the results
  72. echo "Created file "$PWD"/shitpost-"$pref"."$fext""
  73.  
  74. exit 0
  75.  
  76. #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement