-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
250 additions
and
9,268 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ __pycache__/ | |
|
||
# Distribution / packaging | ||
.Python | ||
experiments/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
|
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,95 @@ | ||
import os | ||
import math | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from skimage import io, data, filters | ||
from skimage.viewer import ImageViewer | ||
from skimage import io | ||
from skimage.exposure import histogram | ||
from skimage.measure import label | ||
from skimage.segmentation import flood, flood_fill | ||
from scipy import ndimage | ||
from scipy.ndimage.measurements import label | ||
from functools import reduce | ||
from PIL import Image | ||
import cv2 | ||
|
||
# https://www.youtube.com/watch?v=TyWtx7q2D7Y | ||
# Connected componenets | ||
|
||
def scale_down(img, scalar): | ||
dims = img.shape | ||
height, length = dims[0], dims[1] | ||
return (length/scalar, height/scalar) | ||
|
||
def show(img, scalar=1): | ||
if scalar: | ||
fig = plt.figure(figsize = scale_down(img, scalar)) | ||
else: | ||
fig = plt.figure() | ||
io.imshow(img, aspect='auto') | ||
io.show() | ||
|
||
def centroid(t1,t2): | ||
return (int(sum(t1)/2), int(sum(t2)/2)) | ||
|
||
def read_img(filename): | ||
""" | ||
Function that returns image and integer background | ||
""" | ||
img = io.imread(filename) | ||
return img | ||
|
||
def get_integer_background(img, bcolor=0): | ||
background = flood(img[..., 0], (0,0), tolerance=0.0) | ||
img[background] = bcolor | ||
intback = np.invert(background).astype(int) | ||
return intback | ||
|
||
img = read_img('link.png') | ||
intback = get_integer_background(img) | ||
structure = np.ones((3, 3), dtype=np.int) | ||
labeled, ncomponents = label(intback, structure) | ||
clusters = [] | ||
for n in range(ncomponents): | ||
raw_inds = np.where(labeled==n) | ||
rrow, rcol = raw_inds | ||
# indices = zip(rrow, rcol) | ||
# indices = sorted(indices, key=lambda x:x[1]) | ||
# indices = sorted(indices, key=lambda x:x[0]) | ||
# least_index = indices[0] | ||
minr, maxr = int(min(rrow)), int(max(rrow)) | ||
minc, maxc = int(min(rcol)), int(max(rcol)) | ||
least_index = (maxr,maxc) | ||
comp_dict = { | ||
'index': n, | ||
'row_tup': (minr, maxr+1), | ||
'col_tup': (minc, maxc+1), | ||
'sort_point': least_index | ||
} | ||
#comp_dict['point'] = centroid((minr, maxr+1),(minc, maxc+1)) | ||
#comp_dict['point'] = least_index | ||
#comp_dict['img'] = img[minr:maxr+1,minc:maxc+1] | ||
#sub_image = img[minr:maxr+1,minc:maxc+1] | ||
#show(sub_image,50) | ||
clusters.append(comp_dict) | ||
import fire | ||
from utils import show, unique, filter_bounds, get_bounds | ||
|
||
|
||
def get_chunk(tup, labeled): | ||
i, j = tup | ||
return [unique(r) for r in labeled[i+1:j]] | ||
|
||
def get_chunk_set(chunk): | ||
chunk_sets = list(map(set, chunk)) | ||
return reduce(lambda a,b: a.union(b), chunk_sets) | ||
|
||
|
||
def get_labeled_for_rows(labeled, row_tups): | ||
sprite_rows = [] | ||
for tup in row_tups: | ||
max_row = [] | ||
chunk = get_chunk(tup, labeled) | ||
chunk_set = get_chunk_set(chunk) | ||
sorted_rows = sorted(chunk, key=lambda x: len(x), reverse=True) | ||
if not sorted_rows: | ||
continue | ||
while set(max_row) != chunk_set: | ||
extra = sorted_rows.pop(0) | ||
max_row.extend(extra) | ||
sprite_rows.append(unique(max_row)) | ||
return sprite_rows | ||
|
||
|
||
class SpriteSheet(object): | ||
def __init__(self, filename, background=0): | ||
self.filename = filename | ||
self.foldername = f"{filename.split('.')[0]}_groups" | ||
self.img = io.imread(filename) | ||
self.background=background | ||
self.boolean_image=None | ||
self.num_labels=None | ||
self.labeled_image=None | ||
self._get_boolean_image() | ||
self._get_labeled_image() | ||
|
||
def _get_boolean_image(self): | ||
background = flood(self.img[..., 0], (0,0), tolerance=0.0) | ||
self.img[background] = 0 | ||
self.boolean_image = np.invert(background).astype(int) | ||
|
||
def _get_labeled_image(self): | ||
structure = np.ones((3, 3), dtype=np.int) | ||
labeled, ncomponents = label(self.boolean_image, structure) | ||
self.labeled_image = labeled | ||
self.num_labels = ncomponents | ||
|
||
clusters = sorted(clusters, key=lambda k: k['sort_point'][1]) | ||
clusters = sorted(clusters, key=lambda k: k['sort_point'][0]) | ||
|
||
|
||
|
||
|
||
### POST SORT LOGIC #### | ||
#print(len(clusters)) | ||
for c in clusters[0:20]: | ||
#for sort_n in component_sort_dict: | ||
#c = clusters[sort_n] | ||
#print(c['index'], c['point']) | ||
topleft, topright = c['row_tup'] | ||
botleft, botright = c['col_tup'] | ||
sub_image = img[topleft:topright,botleft:botright] | ||
#io.imsave(f'tmp/img{n}.png',sub_image) | ||
# print(c['point']) | ||
show(sub_image,50) | ||
|
||
|
||
##### Sub Div stuff ###### | ||
# def get_diff_list(rows): | ||
# diffrows = [None] | ||
# for i in range(len(rows)-1): | ||
# if rows[i+1]-rows[i] ==1: | ||
# continue | ||
# else: | ||
# if diffrows[-1]!=rows[i]: | ||
# diffrows.append(rows[i]) | ||
# diffrows.append(rows[i+1]) | ||
# return diffrows[1:] | ||
|
||
# rows = [] | ||
# for row in range(len(img)): | ||
# num_colors = len(set(list(tuple(x) for x in img[row]))) | ||
# if num_colors == 1: | ||
# rows.append(row) | ||
# print(rows) | ||
|
||
# diffrows = get_diff_list(rows) | ||
# col_dict = {} | ||
# for i in range(len(diffrows)-1): | ||
# up, down = diffrows[i],diffrows[i+1] | ||
# for col in range(len(img[0])): | ||
# num_colors = len(set(list(tuple(x) for x in img[up:down,col]))) | ||
# if num_colors == 1: | ||
# if (up,down) not in col_dict: | ||
# col_dict[(up,down)] = [] | ||
# col_dict[(up,down)].append(col) | ||
def _get_sprite_groups(self): | ||
print(self.boolean_image) | ||
bound_tups = filter_bounds(get_bounds(self.boolean_image)) | ||
sprite_groups = get_labeled_for_rows(self.labeled_image, bound_tups) | ||
return sprite_groups | ||
|
||
#TODO allow for custom foldernames | ||
def _check_create_folder(self,foldername=None): | ||
foldername = foldername if foldername else self.foldername | ||
if foldername not in set(os.listdir()): | ||
os.mkdir(foldername) | ||
return foldername | ||
|
||
# for rbound in rows: | ||
# img[rbound,:] = 255 | ||
|
||
# for tup,col in col_dict.items(): | ||
# up,down = tup | ||
# img[up:down,col] = 255 | ||
|
||
# row_dict = {} | ||
# for tup in col_dict: | ||
# up,down = tup | ||
# bound_cols = get_diff_list(col_dict[tup]) | ||
# for i in range(len(bound_cols)-1): | ||
# left,right = bound_cols[i], bound_cols[i+1] | ||
# for row in range(up,down+1): | ||
# num_colors = len(set(list(tuple(x) for x in img[row,left:right]))) | ||
# if num_colors == 1: | ||
# if row not in row_dict: | ||
# row_dict[row] = [] | ||
# row_dict[row] = (left,right) | ||
# #img[row,left:right] = 0 | ||
|
||
# for row,tup in row_dict.items(): | ||
# left,right = tup | ||
# img[row,left:right] = 255 | ||
# show(img,50) | ||
|
||
|
||
|
||
|
||
def save(self): | ||
sprite_groups = self._get_sprite_groups() | ||
self._check_create_folder() | ||
for group_num, group in enumerate(sprite_groups): | ||
group_foldername = f"{self.foldername}/group_{group_num}" | ||
self._check_create_folder(group_foldername) | ||
for i, label in enumerate(group): | ||
filename = f"{group_foldername}/g{group_num}_{i}.png" | ||
raw_inds = np.where(self.labeled_image==label) | ||
rrow, rcol = raw_inds | ||
minr, maxr = int(min(rrow)), int(max(rrow)) | ||
minc, maxc = int(min(rcol)), int(max(rcol)) | ||
sub_image = self.img[minr:maxr+1,minc:maxc+1] | ||
#show(sub_image,50) | ||
io.imsave(filename,sub_image) | ||
|
||
if __name__ == '__main__': | ||
fire.Fire(SpriteSheet) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import os | ||
import math | ||
import numpy as np | ||
from skimage import io | ||
from skimage.exposure import histogram | ||
from skimage.measure import label | ||
from skimage.segmentation import flood, flood_fill | ||
from scipy import ndimage | ||
from scipy.ndimage.measurements import label | ||
from functools import reduce | ||
from PIL import Image | ||
import cv2 | ||
import fire | ||
from utils import show, unique, filter_bounds, get_bounds | ||
|
||
|
||
def get_chunk(tup, labeled): | ||
i, j = tup | ||
return [unique(r) for r in labeled[i+1:j]] | ||
|
||
def get_chunk_set(chunk): | ||
chunk_sets = list(map(set, chunk)) | ||
return reduce(lambda a,b: a.union(b), chunk_sets) | ||
|
||
|
||
def get_labeled_for_rows(labeled, row_tups): | ||
sprite_rows = [] | ||
for tup in row_tups: | ||
max_row = [] | ||
chunk = get_chunk(tup, labeled) | ||
chunk_set = get_chunk_set(chunk) | ||
sorted_rows = sorted(chunk, key=lambda x: len(x), reverse=True) | ||
if not sorted_rows: | ||
continue | ||
while set(max_row) != chunk_set: | ||
extra = sorted_rows.pop(0) | ||
max_row.extend(extra) | ||
sprite_rows.append(unique(max_row)) | ||
return sprite_rows | ||
|
||
|
||
class SpriteSheet(object): | ||
def __init__(self, filename, background=0): | ||
self.filename = filename | ||
self.foldername = f"{filename.split('.')[0]}_groups" | ||
self.img = io.imread(filename) | ||
self.background=background | ||
self.boolean_image=None | ||
self.num_labels=None | ||
self.labeled_image=None | ||
self._get_boolean_image() | ||
self._get_labeled_image() | ||
|
||
def _get_boolean_image(self): | ||
background = flood(self.img[..., 0], (0,0), tolerance=0.0) | ||
self.img[background] = 0 | ||
self.boolean_image = np.invert(background).astype(int) | ||
|
||
def _get_labeled_image(self): | ||
structure = np.ones((3, 3), dtype=np.int) | ||
labeled, ncomponents = label(self.boolean_image, structure) | ||
self.labeled_image = labeled | ||
self.num_labels = ncomponents | ||
|
||
def _get_sprite_groups(self): | ||
print(self.boolean_image) | ||
bound_tups = filter_bounds(get_bounds(self.boolean_image)) | ||
sprite_groups = get_labeled_for_rows(self.labeled_image, bound_tups) | ||
return sprite_groups | ||
|
||
#TODO allow for custom foldernames | ||
def _check_create_folder(self,foldername=None): | ||
foldername = foldername if foldername else self.foldername | ||
if foldername not in set(os.listdir()): | ||
os.mkdir(foldername) | ||
return foldername | ||
|
||
def save(self): | ||
sprite_groups = self._get_sprite_groups() | ||
self._check_create_folder() | ||
for group_num, group in enumerate(sprite_groups): | ||
group_foldername = f"{self.foldername}/group_{group_num}" | ||
self._check_create_folder(group_foldername) | ||
for i, label in enumerate(group): | ||
filename = f"{group_foldername}/g{group_num}_{i}.png" | ||
raw_inds = np.where(self.labeled_image==label) | ||
rrow, rcol = raw_inds | ||
minr, maxr = int(min(rrow)), int(max(rrow)) | ||
minc, maxc = int(min(rcol)), int(max(rcol)) | ||
sub_image = self.img[minr:maxr+1,minc:maxc+1] | ||
#show(sub_image,50) | ||
io.imsave(filename,sub_image) | ||
|
||
if __name__ == '__main__': | ||
fire.Fire(SpriteSheet) | ||
|
||
# img = io.imread('link.png') | ||
# background = flood(img[..., 0], (0,0), tolerance=0.0) | ||
# img[background] = 0 | ||
# show(img, 50) | ||
# intback = np.invert(background).astype(int) | ||
|
||
# structure = np.ones((3, 3), dtype=np.int) | ||
# labeled, ncomponents = label(intback, structure) | ||
|
||
# bound_tups = filter_bounds(get_bounds(intback)) | ||
# labeled_sprites = get_labeled_for_rows(labeled, bound_tups) | ||
|
||
# # print('----') | ||
# # n = 0 | ||
# # for r in labeled_sprites: | ||
# # n += len(r) | ||
# # print(ncomponents, n) | ||
|
||
# for r, row in enumerate(labeled_sprites): | ||
# print(row) | ||
# for i, n in enumerate(row): | ||
# filename = f"tmp/g{r}_{i}.png" | ||
# raw_inds = np.where(labeled==n) | ||
# rrow, rcol = raw_inds | ||
# minr, maxr = int(min(rrow)), int(max(rrow)) | ||
# minc, maxc = int(min(rcol)), int(max(rcol)) | ||
# sub_image = img[minr:maxr+1,minc:maxc+1] | ||
# #show(sub_image,50) | ||
# io.imsave(filename,sub_image) |
Binary file not shown.
File renamed without changes
Oops, something went wrong.