telegram-stickers.bash 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. ## Kopimi 2015 - this file has no license to begin with
  3. ## Source code should be at:
  4. ## https://notabug.org/desci/scripts/src/master/telegram-stickers.bash
  5. ##
  6. ## This script converts the given image to a png format accepted by telegram's
  7. ## stickers bot (https://telegram.me/stickers).
  8. ## You need ImageMagick for this to work.
  9. ##
  10. ## To convert all images in a directory, you could try something like this:
  11. ## for IMAGE in *.gif *.jpg; do bash telegram-stickers.bash ${IMAGE}; done
  12. ##
  13. if [ ! -z $1 ]
  14. then
  15. ## This test whether the first argument is an image.
  16. if [ "`file -ib $1 | grep -e '^image/'`" != "" ]
  17. then
  18. IMAGE="${1}"
  19. NEW_IMAGE="`basename ${IMAGE} | sed -s 's/\.[A-Za-z]*$/\.png/'`"
  20. WIDTH=`identify -format %w "${IMAGE}"`
  21. HEIGHT=`identify -format %h "${IMAGE}"`
  22. ## We need to know whether we have to reduce the width or the height.
  23. if [ ${WIDTH} -gt ${HEIGHT} ]
  24. then
  25. convert -resize 512 "${IMAGE}" "${NEW_IMAGE}"
  26. echo "${IMAGE} converted to 512 width, automagically adjusted height. Saved to ${NEW_IMAGE}"
  27. elif [ ${WIDTH} -lt ${HEIGHT} ]
  28. then
  29. convert -resize x512 "${IMAGE}" "${NEW_IMAGE}"
  30. echo "${IMAGE} converted to 512 height, automagically adjusted width. Saved to ${NEW_IMAGE}"
  31. elif [ ${WIDTH} -eq ${HEIGHT} ]
  32. then
  33. convert -resize 512x512 "${IMAGE}" "${NEW_IMAGE}"
  34. echo "${IMAGE} converted to 512 width and 512 height. Saved to ${NEW_IMAGE}"
  35. else
  36. echo "I'm not sure what to do with ${IMAGE}."
  37. fi
  38. else
  39. echo "${1}: Not an image. Or so I decided."
  40. fi
  41. else
  42. echo -e "Example of usage: ${0} image.png"
  43. fi