Last active
August 29, 2015 14:06
-
-
Save ahornerr/6709382277e0c28a7436 to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
from PIL import Image | |
import glob | |
import json | |
def get_tile(image_file, size, shade): | |
img = Image.open(image_file) | |
shade = 1 - shade | |
shaded = img.copy().convert('RGB') | |
alpha = img | |
shaded = Image.eval(shaded, lambda x: int(x * shade)) | |
alphad = Image.composite(shaded, Image.new('RGBA', shaded.size, (0, 0, 0, 0)), alpha) | |
return alphad.resize((size, size)) | |
def left_deform(tile, mult = 0.5, hinc = 1.25): | |
tile = tile.resize((tile.size[0], int(tile.size[1] * hinc))) | |
w, h = tile.size | |
img = Image.new('RGBA', (w, int(h * (1 + mult))), (0, 0, 0, 0)) | |
y = 0 | |
for x in range(w): | |
y += mult | |
yy = int(y) | |
crp = tile.crop((x, 0, x + 1, h)) | |
img.paste(crp, (x, yy)) | |
return img | |
def right_deform(tile, mult = 0.5, hinc = 1.25): | |
ow, oh = tile.size | |
tile = tile.resize((oh, int(ow * hinc))) | |
w, h = tile.size | |
img = Image.new('RGBA', (w, int(h * (1 + mult))), (0, 0, 0, 0)) | |
y = int(w * 0.5) | |
for x in range(w): | |
y -= mult | |
yy = int(y) | |
crp = tile.crop((x, 0, x + 1, h)) | |
img.paste(crp, (x, yy)) | |
return img | |
def top_deform(tile): | |
print tile.size | |
w = tile.size[0] * 2 | |
h = tile.size[1] * 2 | |
r = left_deform(tile, 1, 1).rotate(90) | |
return left_deform(r, 0.5, 1).crop((0, int(h // 4), w, h)) | |
def render_cube(ltile, rtile, ttile, fudge = 0.335): | |
w, h = int(ltile.size[0]), int(ltile.size[1] * 1.5) | |
ltile = left_deform(ltile) | |
rtile = right_deform(rtile) | |
ttile = top_deform(ttile) | |
img = Image.new('RGBA', (int(w * 2), int(h * 1.5)), (0, 0, 0, 0)) | |
img.paste(ttile, (0, 0), ttile) | |
img.paste(ltile, (0, int(h * fudge)), ltile) | |
img.paste(rtile, (w, int(h * fudge)), rtile) | |
return img | |
with open('settings.json') as fid: | |
settings = json.load(fid) | |
blocks = {} | |
for file_name in glob.glob('blocks/*png'): | |
fname = file_name.replace(".png", "").replace("blocks\\", "").replace("blocks/", "") | |
name_split = fname.split("_") | |
last_word_is_special = name_split[-1] in {"side", "right", "left", "top"} | |
special_word = name_split[-1] | |
if last_word_is_special: | |
name_split.remove(special_word) | |
real_name = "_".join(name_split) | |
if real_name in blocks: | |
blocks[real_name].append(special_word) | |
else: | |
blocks[real_name] = [special_word] | |
for block_name, sides in blocks.iteritems(): | |
print block_name, sides | |
left_image = "blocks/" + block_name + ".png" | |
right_image = "blocks/" + block_name + ".png" | |
top_image = "blocks/" + block_name + ".png" | |
if "top" in sides: | |
top_image = "blocks/" + block_name + "_top.png" | |
if "left" in sides: | |
left_image = "blocks/" + block_name + "_left.png" | |
if "right" in sides: | |
right_image = "blocks/" + block_name + "_right.png" | |
if "side" in sides: | |
left_image = "blocks/" + block_name + "_side.png" | |
right_image = "blocks/" + block_name + "_side.png" | |
ltile = get_tile(left_image, settings['size'], settings['shading amount'] / 2) | |
rtile = get_tile(right_image, settings['size'], settings['shading amount']) | |
ttile = get_tile(top_image, settings['size'], 0) | |
block = render_cube(ltile, rtile, ttile, settings['fudge']) | |
if not os.path.exists("render"): | |
os.mkdir("render") | |
block.save('render/' + block_name + '.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment