Skip to content

Instantly share code, notes, and snippets.

@bhautikj
Created September 20, 2020 23:19
Show Gist options
  • Save bhautikj/2375a205027e4307cd644d5d0201d953 to your computer and use it in GitHub Desktop.
Save bhautikj/2375a205027e4307cd644d5d0201d953 to your computer and use it in GitHub Desktop.
Testing crop algorithm on twitter
#
# Written for python
# pip install tweepy pillow
#
import tweepy
from PIL import Image, ImageFilter
import glob, os, sys, time
SLEEPTIME = 3 #in seconds
twitter_auth_keys = {
"consumer_key" : CONSUMER_KEY,
"consumer_secret" : CONSUMER_SECRET,
"access_token" : ACCESS_TOKEN,
"access_token_secret" : ACCESS_SECRET
}
def generateImagePairs(directoryName):
types = ('*.jpg', '*.JPG', '*.png', '*.PNG')
imagePairs = []
imageSet = []
for files in types:
imageSet.extend(glob.glob(os.path.join(directoryName, files)))
blurredSet = []
for image in imageSet:
blurredName = os.path.join(directoryName, "blurred_" + os.path.basename(image).split('.')[0] + ".png")
if "blurred" in image or os.path.isfile(blurredName):
continue
print("blurring", image)
im = Image.open(image)
im = im.filter(ImageFilter.GaussianBlur(radius = 3))
im.save(blurredName)
blurredSet.append(blurredName)
imageSet.extend(blurredSet)
for imageA in imageSet:
for imageB in imageSet:
if imageA == imageB:
continue
pair = set([imageA, imageB])
if pair not in imagePairs:
imagePairs.append(pair)
ret = []
for pair in imagePairs:
dt = {}
lt = list(pair)
dt["A"] = lt[0]
dt["B"] = lt[1]
dt["title"] = os.path.basename(lt[0]).split('.')[0] + " vs " + os.path.basename(lt[1]).split('.')[0]
ret.append(dt)
print("Found", len(imageSet), "images and", len(ret), "pairs")
return ret
def generateImage(pairDict):
imageA = Image.open(pairDict["A"])
imageB = Image.open(pairDict["B"])
img = Image.new('RGB', (512, 1536), color = 'black')
img.paste(imageA, (0,0))
img.paste(imageB, (0,1024))
outFile = "tmp.png"
img.save(outFile)
return outFile
def main(directoryToProcess):
auth = tweepy.OAuthHandler(
twitter_auth_keys['consumer_key'],
twitter_auth_keys['consumer_secret']
)
auth.set_access_token(
twitter_auth_keys['access_token'],
twitter_auth_keys['access_token_secret']
)
api = tweepy.API(auth)
imagePairs = generateImagePairs(directoryToProcess)
imagePairs = sorted(imagePairs, key=lambda x: x["title"])
tweetParent = "In this thread I'm going to test out the cropping algorithm on twitter. Analysis will follow shortly."
post_result = api.update_status(status=tweetParent)
parentID = post_result.id
for pair, i in zip(imagePairs, range(len(imagePairs))):
time.sleep(SLEEPTIME)
ab = pair["title"].split(" vs ")
print("PAIR:", ab[0], ",", ab[1])
print("working on pair", pair["title"], i+1, "of", len(imagePairs))
img = generateImage(pair)
media = api.media_upload(img)
post_result = api.update_status(status=pair["title"], media_ids=[media.media_id], in_reply_to_status_id = parentID)
print("POST:", pair["title"], post_result.id)
if __name__ == "__main__":
directoryToProcess = sys.argv[1]
main(directoryToProcess)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment