This page looks best with JavaScript enabled

Automation: Posting a phrase on twitter and generating an image from it.

 ·   ·   4 min read

Where does this come from? It’s well known that a picture is worth a thousand words, and if it has letters then it should worth even more (I guess). I’ve noticed someone, with 203K followers, who publishes motivational phrases and he’s doing it great… In fact, I follow him and to me it’s cool, he motivated me with just one phrase; but I wouldn’t have paid attention to that phrase if it’d been only text. No. It needed to have a nice color and be an image to capture my attention among so much information. Then I realized that I could do the same, but of course … a programmer’s way. How? Automating the process :B

The idea (By the way, thanks to James Blute for being the inspiration for this post): It is embodied in the following screenshot: 

The idea is to tweet the text and have the same text but as an image, easy.
What do we need?:

  • Something that allows us to generate an image from text: ImageMagick! Period.(It doesn’t have rivals, from my point of view)
  • Something that allows us to tweet and attach a picture: Here we have more than an option, but as I’m in the Ruby vibe, I will use Twitter cli.
  • Something that links both: bash. I love powershell, but anyway. (Hope the owner of this blog doesn’t find it out).

As far as I know , the package ImageMagick comes installed by default in Linux distributions; I can’t remember having to install it. But if I needed to, I think that the next would solve it: 

$ sudo apt-get install imagemagick

Next in our list is twitter cli. The installation process is tedious and worthy of a full post. That’s why I’m leaving the installation link here: [Installation] and the next benefits if you are still wondering whether to install it or not:

  • How funny it is to tweet from the command line: “We are geeks, yeaah baby”.
  • Twwet automatically using cron or at :D. This really is great … At least one follower will wonder if you ever sleep xD
  • And… that.

And now, the important part: the little script in bash.

#!/bin/bash  
echo "This will be posted: $1"  
file='temporary.txt'  
eval \`touch $file\`  
eval \`echo $1>>$file\`  
  
picture=\`date +'%F-%T'\`  
image\_magick="convert   
\-background turquoise   
\-fill white   
\-pointsize 18   
\-size 400x400   
\-gravity center   
label:@$file   
$picture.gif"  
eval $image\_magick  
  
eval \`rm $file\`  
tweet="t update \\"$1\\" --file=$picture.gif"  
eval $tweet  
#eval \`rm $picture.gif\`  
echo "Done boss."  

We copy the above in a file called: post-automatically
We assign execute permissions:

$ chmod +x post-automatically

And run the script passing an argument as a parameter: the text you wish to post, in double or single quotes script. In this case doubles:

$ ./post-automatically "Hi"

And we have the next result:

And so the visualization in Twitter :D

Hi pic.twitter.com/QUlL36pmIS
— Rizel Tane a.k.a Liz (@RizelTane) julio 25, 2014

What does the script do?

It creates a file called: temporary.txt in which we have inserted the text passed as argument. Then it gets the current date and time to create the name of our image.

It then executes the convert command (from the ImageMagick package) with the next options and in this order:

Background color: turquoise

Font color: white

Font size: 18 points

Canvas size: 400x400 pixels

Text alignment: center

Text: The content of our file

Image to be created: the name we have previously generated, with gif extension,

It then removes the file where our text was, because we don’t need it anymore.

Then we tweet the text received as an argument and attach the image that has been generated.

The script came out from a spark of madness … so there are cases where it does not work. I challenge you to find such cases and improve it. I was trying to improve it, I’m still in that, but I’ll do it when I feel that spark of madness again (besides I don’t have that much time, I’m in a startup project), meanwhile below is the script at the development phase. PD. I do not program in bash (This must be my fourth attempt) so my code (which is seen below) can be insulting to some experts.

Regards. 

#!/bin/bash  
echo "This is the text that will be posted: $1"  
#$( echo VAR=value )  
file='temporary.txt'  
eval echo \`touch $file\`  
eval echo \`echo $1>>$file\`  
#now="$(date +'%d/%m/%Y')"  
#whoami  
csv=\`t whoami -c\`  
  
#grep -o . <<< $csv | while read letter;  do echo "my letter is $letter" ; done  
#echo $csv  
  
for (( i=0, c=0; i<${#csv}; i++ )); do  
  if \[\[ ${csv:$i:1} == "," \]\]  
  then  
 let c++  
 if \[ $c == 23 \]   
 then  
   #while   
   #echo ${csv:$i:1}  
   #echo ${csv:$i:2}  
   #echo ${csv:$i:3}  
   #echo ${csv:$i:4}  
   #echo ${csv:$i:5}  
   #echo ${csv:$i:6}  
   #echo ${csv:$i:7}  
   #echo ${csv:$i:9}  
   #echo ${csv:$i:10}  
   #echo ${csv:$i:11}  
   for (( j=1;  ${csv:$i:$j} != ","  ; j++ )); do  
    echo ${csv:$i:$j}  
   done  
 fi  
  fi  
done  
  
  
  
picture=\`date +'%F%T'\`  
image\_magick="convert   
\-background turquoise   
\-fill white   
\-pointsize 18   
\-size 500x500   
\-gravity center   
label:@$file   
$picture.gif"  
eval $image\_magick  
  
  
  
echo "Done boss."  

Share on
Support the author with

Avatar
WRITTEN BY