Skip to content

Instantly share code, notes, and snippets.

@Shoeboxam
Last active August 29, 2015 14:13
Show Gist options
  • Save Shoeboxam/fcd11be633e9a42e3c58 to your computer and use it in GitHub Desktop.
Save Shoeboxam/fcd11be633e9a42e3c58 to your computer and use it in GitHub Desktop.
from PIL import Image
import math
img = Image.open(r"C:\Users\mike_000\Desktop\planks_oak.png")
board_widths = [16, 16, 16, 16]
resize_factor = img.size[0] / 16
fence_side = img.crop([0, 0, img.size[0], img.size[1]])
fence_top = img.crop([0, 0, img.size[0], img.size[1]])
gate_side = img.crop([0, 0, img.size[0], img.size[1]])
gate_top = img.crop([0, 0, img.size[0], img.size[1]])
# img = img.transpose(Image.ROTATE_270)
boards = []
top = 0
bottom = 0
for width in board_widths:
top = bottom
bottom += width
boards.append(img.crop([0, top, img.size[0], bottom]))
def resize(img, size, edge=4):
resized = Image.new('RGBA', size)
# Split apart elements of plank
center = img.crop([edge, edge, img.size[0] - edge, img.size[1] - edge])
top_left = img.crop([0, 0, edge, edge])
top_edge = img.crop([edge, 0, img.size[0] - edge, edge])
top_right = img.crop([img.size[0] - edge, 0, img.size[0], edge])
right_edge = img.crop([img.size[0] - edge, edge, img.size[0], img.size[1] - edge])
bottom_right = img.crop([img.size[0] - edge, img.size[1] - edge, img.size[0], img.size[1]])
bottom_edge = img.crop([edge, img.size[1] - edge, img.size[0] - edge, img.size[1]])
bottom_left = img.crop([0, img.size[1] - edge, edge, img.size[1]])
left_edge = img.crop([0, edge, edge, img.size[1] - edge])
# Stretch horizontal grain
top_edge = top_edge.resize([size[0] - (edge * 2), top_edge.size[1]])
center = center.resize([size[0] - (edge * 2), center.size[1]])
bottom_edge = bottom_edge.resize([size[0] - (edge * 2), bottom_edge.size[1]])
# Repeat vertical grain
temp_height = math.ceil(size[1] - (edge * 2) / img.size[1] - (edge * 2))
left_edge_large = Image.new('RGBA', (edge, temp_height))
center_large = Image.new('RGBA', (center.size[0], temp_height))
right_edge_large = Image.new('RGBA', (edge, temp_height))
for repeat in range(temp_height):
left_edge_large.paste(left_edge, (0, left_edge.size[1] * repeat, edge, left_edge.size[1] * (repeat + 1)))
center_large.paste(center, (0, center.size[1] * repeat, center.size[0], center.size[1] * (repeat + 1)))
right_edge_large.paste(right_edge, (0, right_edge.size[1] * repeat, edge, right_edge.size[1] * (repeat + 1)))
left_edge = left_edge_large.crop([0, 0, left_edge.size[0], size[1] - (edge * 2)])
center = center_large.crop([0, 0, center.size[0], size[1] - (edge * 2)])
right_edge = right_edge_large.crop([0, 0, right_edge.size[0], size[1] - (edge * 2)])
# Put pieces back together
resized.paste(center, (edge, edge, resized.size[0] - edge, resized.size[1] - edge))
resized.paste(top_left, (0, 0, edge, edge))
resized.paste(top_edge, (edge, 0, resized.size[0] - edge, edge))
resized.paste(top_right, (resized.size[0] - edge, 0, resized.size[0], edge))
resized.paste(right_edge, (resized.size[0] - edge, edge, resized.size[0], resized.size[1] - edge))
resized.paste(bottom_right, (resized.size[0] - edge, resized.size[1] - edge, resized.size[0], resized.size[1]))
resized.paste(bottom_edge, (edge, resized.size[1] - edge, resized.size[0] - edge, resized.size[1]))
resized.paste(bottom_left, (0, resized.size[1] - edge, edge, resized.size[1]))
resized.paste(left_edge, (0, edge, edge, resized.size[1] - edge))
return resized
def scale(coordinates):
scaled = []
for element in coordinates:
scaled.append(int(element * resize_factor))
return scaled
# Top Beam
fence_side.paste(resize(boards[1], scale([16, 3])), scale([0, 1, 16, 4]))
# Bottom Beam
fence_side.paste(resize(boards[2], scale([16, 3])), scale([0, 7, 16, 10]))
# Center post
center_post = resize(boards[0], scale([16, 4]))
center_post = center_post.transpose(Image.ROTATE_90)
center_post = center_post.transpose(Image.FLIP_LEFT_RIGHT)
fence_side.paste(center_post, scale([6, 0, 10, 16]))
fence_side.show()
fence_top.paste(resize(boards[2], scale([16, 2])), scale([0, 7, 16, 9]))
vertical_post = resize(boards[1], scale([16, 2])).transpose(Image.ROTATE_270)
fence_top.paste(vertical_post, scale([7, 0, 9, 16]))
fence_top.paste(resize(boards[3], scale([4, 4]), 1), scale([6, 6, 10, 10]))
fence_top.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment