{"id":622,"date":"2018-05-22T07:44:52","date_gmt":"2018-05-22T07:44:52","guid":{"rendered":"http:\/\/muthu.co\/?p=622"},"modified":"2021-05-24T03:49:01","modified_gmt":"2021-05-24T03:49:01","slug":"instagram-quotes-generator-using-python-pil","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/instagram-quotes-generator-using-python-pil\/","title":{"rendered":"Instagram quotes generator using Python PIL"},"content":{"rendered":"\n
I am using Python’s Pillow library for image creation.You can even install it using pip.<\/p>\n\n\n\n
$ pip install Pillow<\/code><\/pre>\n\n\n\nYou can get the detailed installation instructions here<\/a>.<\/p>\n\n\n\nInstagram’s default post size is 612px 612px so first let me create a 612px square white box.<\/p>\n\n\n\n
from PIL import Image, ImageDraw\n\n#variables for image size\nx1 = 612\ny1 = 612\n\nimg = Image.new('RGB', (x1, y1), color = (255, 255, 255))\nd = ImageDraw.Draw(img)\n\n#save the image in the current directory\nimg.save('quote.png')<\/code><\/pre>\n\n\n\nNow lets put our quote inside this white box.<\/p>\n\n\n\n
from PIL import Image, ImageDraw\n\n#variables for image size\nx1 = 612\ny1 = 612\n\n#my quote\nsentence = \"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. -Albert Einstein\"\n\nimg = Image.new('RGB', (x1, y1), color = (255, 255, 255))\nd = ImageDraw.Draw(img)\n\n#create the text at 0,0 location on the image\nd.text((0,0), sentence ,align=\"center\", fill=(100,100,100))\n\n#save the image in the current directory\nimg.save('quote.png')<\/code><\/pre>\n\n\n\nBelow is output image from the above code.<\/p>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\nDoesn’t look Instagram friendly. Lets make some changes to its font and see if it looks anywhere close to better. For this we will use ImageFont class.<\/p>\n\n\n\n
<\/a><\/figure><\/div>\n\n\n\nThe quote isn’t fitting inside my box. I will need some line breaks between the words. And also need to center the quote in the middle of the box. The below code does the trick for me.<\/p>\n\n\n\n
from PIL import Image, ImageDraw, ImageFont\n\n#variables for image size\nx1 = 612\ny1 = 612\n\n#my quote\nsentence = \"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. -Albert Einstein\"\n\n#choose a font\nfnt = ImageFont.truetype('\/Users\/muthukrishna\/Library\/Fonts\/Cinzel-Regular.ttf', 30)\n\nimg = Image.new('RGB', (x1, y1), color = (255, 255, 255))\nd = ImageDraw.Draw(img)\n\n#find the average size of the letter\nsum = 0\nfor letter in sentence:\n sum += d.textsize(letter, font=fnt)[0]\n\naverage_length_of_letter = sum\/len(sentence)\n\n#find the number of letters to be put on each line\nnumber_of_letters_for_each_line = (x1\/1.618)\/average_length_of_letter\nincrementer = 0\nfresh_sentence = ''\n\n#add some line breaks\nfor letter in sentence:\n if(letter == '-'):\n fresh_sentence += '\\n\\n' + letter\n elif(incrementer < number_of_letters_for_each_line):\n fresh_sentence += letter\n else:\n if(letter == ' '):\n fresh_sentence += '\\n'\n incrementer = 0\n else:\n fresh_sentence += letter\n incrementer+=1\n\nprint fresh_sentence\n\n#render the text in the center of the box\ndim = d.textsize(fresh_sentence, font=fnt)\nx2 = dim[0]\ny2 = dim[1]\n\nqx = (x1\/2 - x2\/2)\nqy = (y1\/2-y2\/2)\n\nd.text((qx,qy), fresh_sentence ,align=\"center\", font=fnt, fill=(100,100,100))\n\nimg.save('quote.png')<\/code><\/pre>\n\n\n\nThe output looks like this<\/p>\n\n\n\n