Making a Python based Twitter Bot— Movie Color Palette Generator

Aditya bhadkamkar
3 min readApr 4, 2019

--

I see a lot of posts and accounts on social media platforms which are devoted to the color pallets of movie posters. They are made using editing software like photoshop and consume a lot of time. So i thought it would be great if i could automate the process and learn new programming concepts along the way.

In this article i’ll walk you through to the python twitter bot i made. Everything from gathering data to processing and generating a color palette which has the main colors of an image using K means clustering:

1.Installing libraries

First we import all the necessary libraries.

import requests
import numpy as np
import cv2
import urllib.request
from DominantColors import DominantColors
import urllib
import tweepy
import time

For all the above packages — $ pip install “package_name” and download all of them. The DominantColors package is actually another class, which can be downloaded from: https://github.com/DataStroke99/MovieColorPalette-TwitterBot/blob/master/DominantColors.py

The original implementation was here -https://gist.github.com/skt7/b3d0445ea902c7907c6d005d1079b76a

But then i made few important changes to integrate it to my code. Save it to the same directory and it would be used later on to generate the color pallete.

2.Gathering Data

Now we initialize the variables we would be using. this includes the twitter bot initialization we would be using later. You can get the values for the twitter bot through your twitter developers account and registering for the API. Same goes for the API_KEY of TMDb Database.

Initially i wanted to use the IMDB database but found it caused some problems and was discontinued. There were lot of wrappers available but after some more research i found another great website. TMDb — The Movie Database is a great free online movie database which contains in depth information and a very user friendly API — https://www.themoviedb.org/

# Twitter bot initialization
consumer_key = 'CONSUMER_KEY'
consumer_secret = 'CONSUMER_SECRET'
access_token = 'ACCESS_TOKEN'
access_token_secret = 'ACCESS_TOKEN_SECRET'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)


# Variables
api_key = '?YOUR_API_KEY'
url = 'https://api.themoviedb.org/3/movie/'
urls = []
start = 0



for i in range(start,end):
try:
mov_num = str(i)
JSONcontent = requests.get(url + mov_num + api_key)
response = JSONcontent.json()

base_url = 'https://image.tmdb.org/t/p/w500'
img_url = response['poster_path']
image_url = base_url + img_url

urls.append(image_url)
print(urls)

except:

print("pass")
pass

Once all the variables are in place, we write the code in a for loop. the loop is there to iterate through all the possible movie ids. during building the project i found that few movie id’s were missing and it returned an exception. thus the loop helps to implement our exception catching method and only stores the poster path in the url[] list for movies which actually have the data.

3.Defining functions

Now we define the necessary function:

def url_to_image(url):
resp = urllib.request.urlopen(url)
image = np.array(bytearray(resp.read()))

image = cv2.imdecode(image, cv2.IMREAD_COLOR)

return image

This is a function which actually converts each url from our urls[] list into a image data array that our program can easily read and perform functions on.

4.Generating Color Palette

Here we iterate through each poster path and using our previously defined function convert it to array of data. Here i found the easy and effective way is to actually download the image temporarily. when is then sent to the DominantColors() class — which is our k-means cluster palette generating script.

for url in urls:
image = url_to_image(url)

cv2.imwrite("1.jpg", image)

img = '1.jpg'
clusters = 6

dc = DominantColors(img, clusters)
dc.dominantColors()
color = dc.plotHistogram()

numpy_vertical = np.vstack((color, image))
cv2.imwrite("main.jpg", numpy_vertical)
final = "main.jpg"

Once we get the color palette of our image back we combine it with the original image. then this combined image is saved again for it to be easily located and and tweeted out.

5.Twitter bot

Now we simply add a time.sleep() function. in this case each tweet would be delayed 1 minute , the value can be changed accordingly.

Using the .update_with_media() function a new post with an image is added to our timeline each minute.

#Twitter Bot 

time.sleep(60)

api.update_with_media ( final, "Follow my code! -https://github.com/DataStroke99/MovieColorPalette-TwitterBot " + " #cinema #movie #moviepalette #palette #MovieColor #color #art #computerscience")

Here is the entire main code:

You can change around the code and use it to fit your application. Thanks for reading!

--

--