Created
November 9, 2015 23:28
-
-
Save misterhat/cced4ba1bbaa41f83c0c to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# tiler.py - print a tile matrix based on two images and a tile size | |
# Copyright (C) 2015 Zorian M | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
from math import floor | |
from numbers import Number | |
from os import mkdir, rmdir | |
import sys | |
from PIL import Image | |
import numpy | |
""" | |
Match a tile to a list of other tiles and return the ID. | |
""" | |
def find_tile_id(tiles, needle): | |
for i, tile in enumerate(tiles): | |
if numpy.array_equal(tile, needle): | |
return i | |
else: | |
return -1 | |
if __name__ == '__main__': | |
if len(sys.argv) < 4: | |
print('usage: ./tiler.py <size px> <tilesheet> <source>', | |
file=sys.stderr) | |
sys.exit(1) | |
# size of tiles in px | |
size = int(sys.argv[1]) | |
# location of tiles image | |
tiles_image = sys.argv[2] | |
# location of source image | |
source_image = sys.argv[3] | |
if not isinstance(size, Number) or size <= 0: | |
print('invalid size. size must be int greater than 0', file=sys.stderr) | |
sys.exit(1) | |
try: | |
tiles_image = Image.open(tiles_image) | |
source_image = Image.open(source_image) | |
except FileNotFoundError: | |
print('invalid source or tiles image locations', file=sys.stderr) | |
sys.exit(1) | |
tiles_pixels = numpy.asarray(tiles_image) | |
source_pixels = numpy.asarray(source_image) | |
# store the cropped tiles in a list | |
tiles = [] | |
width = floor(tiles_image.size[0] / size) | |
height = floor(tiles_image.size[1] / size) | |
for j in range(0, height): | |
for i in range(0, width): | |
cropped = tiles_image.crop((i * size, j * size, (i * size) + size, | |
(j * size) + size)) | |
cropped = numpy.asarray(cropped) | |
tiles.append(cropped) | |
width = floor(source_image.size[0] / size) | |
height = floor(source_image.size[1] / size) | |
# create a directory to store any tiles that aren't found | |
not_founds = [] | |
try: | |
mkdir('./not_founds') | |
except: | |
pass | |
for j in range(0, height): | |
for i in range(0, width): | |
cropped = source_image.crop((i * size, j * size, (i * size) + size, | |
(j * size) + size)) | |
tile_id = find_tile_id(tiles, cropped) | |
# use -1 to indicate a tile not on the tilesheet and save it in a | |
# specified directory so the user can add it | |
if tile_id == -1: | |
not_founds.append((i, j)) | |
cropped.save('./not_founds/{0},{1}.png'.format(i, j)) | |
print(tile_id, end=' ') | |
print() | |
if len(not_founds): | |
print('following tiles not found in tilesheet:', file=sys.stderr) | |
print(not_founds, file=sys.stderr) | |
else: | |
rmdir('./not_founds') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment