Skip to content

Commit

Permalink
Supercharged build.py, removed build.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Jan 29, 2018
1 parent 9a1eb73 commit 52f50d7
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 86 deletions.
Binary file removed build.exe
Binary file not shown.
218 changes: 132 additions & 86 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,107 +1,153 @@
#!/usr/bin/env python
from PIL import Image, ImageDraw
import glob
import random
import colorsys
import argparse
import os

DESKTOP_WIDTH = 1920
DESKTOP_HEIGHT = 1080

TILE_WIDTH = 64
TILE_HEIGHT = 64

TILES_X = int(DESKTOP_WIDTH / TILE_WIDTH)
TILES_Y = int(DESKTOP_HEIGHT / TILE_HEIGHT)

found_count = 0
found_total = TILES_X * TILES_Y

def place_tiles():
global found_count
grid = [[None for y in range(TILES_Y)] for x in range(TILES_X)]
random_tiles = []

tiles = []
for ext in ['png', 'jpg']:
tiles.extend(glob.glob("tiles/*.{}".format(ext)))

found_count = len(tiles)

for tile in tiles:
img = Image.open(tile).convert("RGBA").resize((TILE_WIDTH, TILE_HEIGHT), resample=Image.BILINEAR)

filename = tile.split('/')[-1]
fname, fext = filename.split('.')
for delimiter in ['x','-','_','X']:
if delimiter in fname:
fname = fname.split(delimiter)
break

if len(fname) == 2:
try:
x = int(fname[0])
y = int(fname[1])
print("Adding {} to {}x{}".format(filename, x, y))
grid[x][y] = img
continue
except (ValueError, IndexError):
pass

print("Adding {} to random".format(filename))
random_tiles.append(img)

for random_tile in random_tiles:

while has_slot(grid):
x = random.randint(0, TILES_X-1)
y = random.randint(0, TILES_Y-1)
if grid[x][y] is None:
grid[x][y] = random_tile
break

return grid

def has_slot(grid):
for x in range(TILES_X):
for y in range(TILES_X):
if grid[x][y] is None:
return True
return False
DEFAULT_WIDTH = 1920
DEFAULT_HEIGHT = 1080

if __name__ == "__main__":
wallpaper = Image.new("RGBA", (DESKTOP_WIDTH, DESKTOP_HEIGHT), (0, 0, 0, 255))
DEFAULT_TILE_WIDTH = 64
DEFAULT_TILE_HEIGHT = 64

DEFAULT_ADD_RAINBOWS = True

class Wallpaperer():
def __init__(self, wallpaper_width=1920, wallpaper_height=1080, tile_width=64, tile_height=64, verbose=False):
self.found_count = 0
self.wallpaper_width = wallpaper_width
self.wallpaper_height = wallpaper_height
self.tile_width = tile_width
self.tile_height = tile_height
self.verbose = verbose

@property
def tiles_x(self):
return self.wallpaper_width // self.tile_width

draw = ImageDraw.Draw(wallpaper)
@property
def tiles_y(self):
return self.wallpaper_height // self.tile_height

for y in range(TILES_Y):
val = (1.0 / TILES_Y) * y
for x in range(TILES_X):
hue = (1.0 / TILES_X) * x
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1.0, val)]
rainbow_x = x * TILE_WIDTH
rainbow_y = y * TILE_HEIGHT
draw.rectangle((rainbow_x, rainbow_y, rainbow_x+TILE_WIDTH, rainbow_y+TILE_HEIGHT), fill=(r, g, b, 255), outline=None)
@property
def tile_count(self):
return self.tiles_x * self.tiles_y

grid = place_tiles()
def place_tiles(self, directory="tiles"):
grid = [[None for y in range(self.tiles_y)] for x in range(self.tiles_x)]
random_tiles = []

for x in range(TILES_X):
for y in range(TILES_Y):
offset_x = x * TILE_WIDTH
offset_y = y * TILE_HEIGHT
tile = grid[x][y]
tiles = []
for ext in ['png', 'jpg', 'jpeg']:
tiles.extend(glob.glob(os.path.join(directory, "*.{}".format(ext))))

self.found_count = len(tiles)

for tile in tiles:
img = Image.open(tile).convert("RGBA").resize((self.tile_width, self.tile_height), resample=Image.BILINEAR)

filename = tile.split('/')[-1]
fname, fext = filename.split('.')
for delimiter in ['x','-','_','X']:
if delimiter in fname:
fname = fname.split(delimiter)
break

if len(fname) == 2:
try:
x = int(fname[0])
y = int(fname[1])
if self.verbose: print("Adding {} to {}x{}".format(filename, x, y))
grid[x][y] = img
continue
except (ValueError, IndexError):
pass

if self.verbose: print("Adding {} to random".format(filename))
random_tiles.append(img)

for random_tile in random_tiles:
while self.has_slot(grid):
x = random.randint(0, self.tiles_x-1)
y = random.randint(0, self.tiles_y-1)
if grid[x][y] is None:
grid[x][y] = random_tile
break

return grid

def has_slot(self, grid):
for x in range(self.tiles_x):
for y in range(self.tiles_y):
if grid[x][y] is None:
return True
return False

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate tiled wallpaper!')
parser.add_argument('--rainbows', type=bool, help='add rainbows!', default=DEFAULT_ADD_RAINBOWS)
parser.add_argument('--tiles_x', type=int, help='number of horizontal tiles, overrides width/height', default=None)
parser.add_argument('--tiles_y', type=int, help='number of vertical tiles, overrides width/height', default=None)
parser.add_argument('--tilewidth', type=int, help='tile width in pixels (default: {}px)'.format(DEFAULT_TILE_WIDTH), default=DEFAULT_TILE_WIDTH)
parser.add_argument('--tileheight', type=int, help='tile height in pixels (default: {}px)'.format(DEFAULT_TILE_HEIGHT), default=DEFAULT_TILE_HEIGHT)
parser.add_argument('--width', type=int, help='wallpaper width in pixels (default: {}px)'.format(DEFAULT_WIDTH), default=DEFAULT_WIDTH)
parser.add_argument('--height', type=int, help='wallpaper height in pixels (default: {}px)'.format(DEFAULT_HEIGHT), default=DEFAULT_HEIGHT)
parser.add_argument('--insanity', action='store_true', help='use a proper coordinate system for geniuses! (default: your fault!)', default=False)
parser.add_argument('--filename', type=str, help='filename to save as (defaut: wallpaper.png)', default='wallpaper.png')

args = parser.parse_args()

if args.tiles_x is not None and args.tiles_y is not None:
args.tilewidth = args.width // args.tiles_x
args.tileheight = args.height // args.tiles_y

wallpaperer = Wallpaperer(
wallpaper_width=args.width,
wallpaper_height=args.height,
tile_width=args.tilewidth,
tile_height=args.tileheight)

wallpaper = Image.new("RGBA", (args.width, args.height), (0, 0, 0, 255))

if args.rainbows:
draw = ImageDraw.Draw(wallpaper)

for y in range(wallpaperer.tiles_x):
val = (1.0 / wallpaperer.tiles_y) * y
for x in range(wallpaperer.tiles_x):
hue = (1.0 / wallpaperer.tiles_y) * x
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1.0, val)]
rainbow_x = x * wallpaperer.tile_width
rainbow_y = y * wallpaperer.tile_height
draw.rectangle((rainbow_x, rainbow_y, rainbow_x+wallpaperer.tile_width, rainbow_y+wallpaperer.tile_height), fill=(r, g, b, 255), outline=None)

grid = wallpaperer.place_tiles()

for x in range(wallpaperer.tiles_x):
for y in range(wallpaperer.tiles_y):
offset_x = x * wallpaperer.tile_width
offset_y = y * wallpaperer.tile_height

if args.insanity:
tile = grid[x][y]
else:
tile = grid[x][wallpaperer.tiles_y - 1 - y]

if tile is None:
row = y % 2
col = (x + row) % 2
grey = 5 + (5 * col)
tile = Image.new("RGBA", (TILE_WIDTH, TILE_HEIGHT), (255, 255, 255, grey))
tile = Image.new("RGBA", (wallpaperer.tile_width, wallpaperer.tile_height), (255, 255, 255, grey))

wallpaper.paste(tile, (offset_x, offset_y), mask=tile)

print("Found {found} tiles out of a {total} total. {remaining} tiles left!\nProgress: {prog:1.1f}%".format(
found=found_count,
total=found_total,
remaining=found_total-found_count,
prog=(100.0/found_total) * found_count
found=wallpaperer.found_count,
total=wallpaperer.tile_count,
remaining=wallpaperer.tile_count-wallpaperer.found_count,
prog=(100.0/wallpaperer.tile_count) * wallpaperer.found_count
))

wallpaper.save("wallpaper.png")
wallpaper.save(args.filename)

0 comments on commit 52f50d7

Please sign in to comment.