Skip to content

Instantly share code, notes, and snippets.

@jebeaudet
Created January 29, 2020 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jebeaudet/de5eec5c5ff996d84c184167c47283c3 to your computer and use it in GitHub Desktop.
Save jebeaudet/de5eec5c5ff996d84c184167c47283c3 to your computer and use it in GitHub Desktop.
from PIL import Image
import sys
import requests
from bs4 import BeautifulSoup
if len(sys.argv) <= 3:
print "Please provide the filename of the picture as first argument and the cookie of slack as second argument and the prefix"
exit(1)
max_width_or_height = 3840
img = Image.open(sys.argv[1])
imageWidth, imageHeight = img.size
newImageWidth = (imageWidth/128 + (1 if imageWidth % 128 > 64 else 0)) * 128
newImageHeight = (imageHeight/128 + (1 if imageHeight % 128 > 64 else 0)) * 128
if newImageWidth > max_width_or_height or newImageHeight > max_width_or_height:
if newImageWidth > newImageHeight:
newImageWidth = max_width_or_height
newImageHeight = int(float(imageHeight)/imageWidth * max_width_or_height)
newImageHeight = (newImageHeight/128 + (1 if newImageHeight % 128 > 64 else 0)) * 128
else:
newImageHeight = max_width_or_height
newImageWidth = int(float(imageWidth)/imageHeight * max_width_or_height)
newImageWidth = (newImageWidth/128 + (1 if newImageWidth % 128 > 64 else 0)) * 128
resized_img = img.resize((newImageWidth, newImageHeight))
filenames = []
for i in range(0, newImageWidth, 128):
for j in range(0, newImageHeight, 128):
box = (i, j, i + 128, j + 128)
cropped = resized_img.crop(box)
filename = sys.argv[3] + str(j / 128) + str(i / 128) + ".png"
cropped.save(filename)
filenames.append(filename)
counter = 0
if len(sys.argv) == 4:
for filename in filenames:
counter += 1
print "Uploading tile " + str(counter) + " of " + str(len(filenames))
session = requests.session()
session.headers = {'Cookie' : sys.argv[2]}
files = {"image": (open(filename, "rb"),"image/png"), "mode": "data", "name": filename.replace(".png","")}
response = session.post("https://coveo.slack.com/api/emoji.add", files=files, allow_redirects=False)
print response
string_list = []
x = 0
y = 0
for i in range(0, newImageHeight, 128):
for j in range(0, newImageWidth, 128):
string_list.append(":" + sys.argv[3] + str(x) + str(y) + ":")
y += 1
string_list.append("\n")
y = 0
x += 1
print "".join(string_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment