Created
August 31, 2016 14:00
-
-
Save blainegarrett/9348347b28f138d7b8327c3f2ea49d69 to your computer and use it in GitHub Desktop.
Instagram Assemble
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Initial draft of helper code to create an 700x700 instagrammable image coponsed of other cover images. | |
""" | |
from PIL import Image | |
from PIL import ImageFont | |
from PIL import ImageDraw | |
from math import ceil | |
def readImage(img_filename): | |
im = Image.open(img_filename) | |
return im | |
# Constants | |
SIZE_SMALL = 'small' | |
SIZE_LARGE = 'large' | |
SIZE_TINY = 'tiny' | |
SCALE_FACTOR = {SIZE_LARGE: 1, SIZE_SMALL: 2, SIZE_TINY: 4} | |
ANTIALIAS_SCALE = 4 | |
CANVAS_WIDTH = 700 | |
CANVAS_HEIGHT = 700 | |
MAX_ROW_WIDTH = CANVAS_WIDTH * ANTIALIAS_SCALE | |
def main(): | |
# Define Data set | |
data = [ | |
{'filename': 'pic2.png', 'source_dims': (367, 700), 'size': SIZE_SMALL}, | |
{'filename': 'pic3.png', 'source_dims': (367, 700), 'size': SIZE_SMALL}, | |
{'filename': 'pic3.png', 'source_dims': (367, 700), 'size': SIZE_TINY}, | |
{'filename': 'pic2.png', 'source_dims': (367, 700), 'size': SIZE_TINY}, | |
{'filename': 'pic3.png', 'source_dims': (367, 700), 'size': SIZE_TINY}, | |
{'filename': 'pic2.png', 'source_dims': (367, 700), 'size': SIZE_TINY}, | |
{'filename': 'pic3.png', 'source_dims': (367, 700), 'size': SIZE_LARGE}, | |
] | |
""" | |
{'filename': 'pic2.png', 'source_dims': (367, 700), 'size': SIZE_SMALL}, | |
{'filename': 'pic3.png', 'source_dims': (367, 700), 'size': SIZE_SMALL}, | |
{'filename': 'pic1.png', 'source_dims': (367, 700), 'size': SIZE_LARGE}, | |
{'filename': 'pic3.png', 'source_dims': (367, 700), 'size': SIZE_SMALL}, | |
{'filename': 'pic2.png', 'source_dims': (367, 700), 'size': SIZE_SMALL}, | |
{'filename': 'pic1.png', 'source_dims': (367, 700), 'size': SIZE_LARGE}, | |
{'filename': 'pic2.png', 'source_dims': (367, 700), 'size': SIZE_SMALL}, | |
""" | |
canvas = Image.new("RGB", (CANVAS_WIDTH * ANTIALIAS_SCALE, CANVAS_HEIGHT * ANTIALIAS_SCALE), | |
"black") | |
offsetX = 0 | |
offsetY = 0 | |
bottomX = 0 | |
bottomY = 0 | |
cur_row_used = 0 | |
for im_data in data: | |
# Open up image n | |
p1 = readImage(im_data['filename']) | |
# Calculate final scaled size | |
scale_factor = float(SCALE_FACTOR[im_data['size']]) | |
scaled_width = int(ceil(float(im_data['source_dims'][1]) / scale_factor * float(ANTIALIAS_SCALE))) | |
scaled_height = int(ceil(float(im_data['source_dims'][0]) / scale_factor * float(ANTIALIAS_SCALE))) | |
#p1.thumbnail((scaled_width, scaled_height), Image.ANTIALIAS) | |
p1 = p1.resize((scaled_width, scaled_height), Image.ANTIALIAS) | |
print(p1.size) | |
if (scaled_width + cur_row_used > MAX_ROW_WIDTH): | |
print('NEWLINE (' + str(scaled_width) + ')') | |
# Put it on a new row | |
offsetX = 0 # Left edge of image | |
# offsetY = offsetY + scaled_height | |
bottomY = scaled_height + offsetY # 367 + 183 | |
bottomX = scaled_width | |
paste_tuple = (offsetX, offsetY, bottomX, bottomY) | |
cur_row_used = bottomX | |
else: | |
# offsetY = bottomY | |
offsetX = bottomX | |
bottomX = scaled_width + offsetX # 700, 350 | |
bottomY = scaled_height + offsetY # 367 | |
paste_tuple = (offsetX, offsetY, bottomX, bottomY) | |
cur_row_used = bottomX | |
if (cur_row_used >= MAX_ROW_WIDTH): | |
offsetY = bottomY | |
print([paste_tuple, cur_row_used]) | |
canvas.paste(p1, paste_tuple) | |
# Write text in remainng space | |
draw = ImageDraw.Draw(canvas) | |
# font = ImageFont.truetype(<font-file>, <font-size>) | |
# font = ImageFont.truetype("arial.ttf", 20) | |
font = ImageFont.truetype("BryantWebBold.ttf", 20 * ANTIALIAS_SCALE) | |
# draw.text((x, y),"Sample Text",(r,g,b)) | |
draw.text((20 * ANTIALIAS_SCALE, 660 * ANTIALIAS_SCALE), "Follow #mplsart for more of this week's events", (255, 255, 255), | |
font=font) | |
# >>> img.save('sample-out.jpg') | |
canvas = canvas.resize((CANVAS_WIDTH, CANVAS_HEIGHT), Image.ANTIALIAS) | |
canvas.show() | |
""" | |
# Open up image n | |
p1 = readImage('pic2.png') | |
p0.paste(p1, (0, 0)) | |
# Open image 2 | |
p2 = readImage('pic1.png') | |
p2.thumbnail((350, 183)) | |
print p2.size | |
offsetX = 0 | |
offsetY = 367 | |
bottomX = p2.size[0] + offsetX | |
bottomY = p2.size[1] + offsetY | |
p0.paste(p2, (offsetX, offsetY, bottomX, bottomY)) | |
# Open Image 2: | |
p3 = readImage('pic3.png') | |
p3.thumbnail((350, 183)) | |
print p3.size | |
offsetX = offsetX + p2.size[0] # previous offsetX | |
offsetY = 367 | |
bottomX = p3.size[0] + offsetX | |
bottomY = p3.size[1] + offsetY | |
p0.paste(p3, (offsetX, offsetY, bottomX, bottomY)) | |
p0.show() | |
""" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment