{"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\n

You can get the detailed installation instructions here<\/a>.<\/p>\n\n\n\n

Instagram’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\n

Now 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\n

Below is output image from the above code.<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

Doesn’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\n

The 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\n

The output looks like this<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

Another one on black background:<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"

I am using Python’s Pillow library for image creation.You can even install it using pip. You can get the detailed installation instructions here. Instagram’s default post size is 612px  612px so first let me create a 612px square white box. Now lets put our quote inside this white box. Below is output image from the […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[58],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/622"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=622"}],"version-history":[{"count":3,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/622\/revisions"}],"predecessor-version":[{"id":1900,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/622\/revisions\/1900"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}