Last active
June 7, 2020 14:07
-
-
Save sn0opy/dceaf635a3d73902773b3fc557a7a7dd to your computer and use it in GitHub Desktop.
generate mon icons in several formats
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 cv2 | |
import numpy as np | |
import os | |
# original asset mon icon path | |
assets = "../PogoAssets/pokemon_icons/" | |
def crop(img): | |
channels = cv2.split(img) | |
alpha = channels[3] | |
_, thresh = cv2.threshold(alpha, 127, 255, 0) | |
contours, _ = cv2.findContours(thresh, 1, 2) | |
cnt = contours[-1] | |
xmin, ymin, width, height = cv2.boundingRect(cnt) | |
dim = max(width, height) | |
# return img[ymin:ymin+height, xmin:xmin+width] | |
return img[ymin : ymin + dim, xmin : xmin + dim] | |
def resize(img): | |
res = cv2.resize(img, (30, 30), interpolation=cv2.INTER_LINEAR) | |
return res | |
def fit_to_size(img, size): | |
vertical_missing = size[0] - img.shape[0] | |
horizontal_missing = size[1] - img.shape[1] | |
top_border = vertical_missing // 2 | |
if vertical_missing % 2 == 0: | |
bottom_border = vertical_missing // 2 | |
else: | |
bottom_border = vertical_missing // 2 + 1 | |
left_border = horizontal_missing // 2 | |
if horizontal_missing % 2 == 0: | |
right_border = horizontal_missing // 2 | |
else: | |
right_border = horizontal_missing // 2 + 1 | |
border = cv2.copyMakeBorder( | |
img, | |
top=top_border, | |
bottom=bottom_border, | |
left=left_border, | |
right=right_border, | |
borderType=cv2.BORDER_CONSTANT, | |
value=[0, 0, 0, 0], | |
) | |
return border | |
def outline(img): | |
channels = cv2.split(img) | |
alpha = channels[3] | |
edges = cv2.Canny(alpha, 100, 200) | |
outline = np.argwhere(edges > 0) | |
for i, j in outline: | |
img[i, j] = (0, 0, 0, 255) | |
return img | |
if __name__ == "__main__": | |
for filename in os.listdir(assets): | |
oimg = assets + filename | |
original = cv2.imread(oimg, cv2.IMREAD_UNCHANGED) | |
cropped = crop(original) | |
if cropped.shape[0] > 100 or cropped.shape[1] > 100: | |
max_dim = max(cropped.shape[0], cropped.shape[1]) | |
ratio = 100 / max_dim | |
dim = (int(ratio * cropped.shape[1]), int(ratio * cropped.shape[0])) | |
cropped = cv2.resize(cropped, dim, cv2.INTER_LINEAR) | |
if cropped.shape[0] > 50 or cropped.shape[1] > 50: | |
max_dim = max(cropped.shape[0], cropped.shape[1]) | |
ratio = 50 / max_dim | |
dim = (int(ratio * cropped.shape[1]), int(ratio * cropped.shape[0])) | |
small = cv2.resize(cropped, dim, cv2.INTER_LINEAR) | |
small = fit_to_size(small, (50, 50)) | |
cv2.imwrite("50x50/{}".format(filename), small) | |
large = fit_to_size(cropped, (100, 100)) | |
cv2.imwrite("100x100/{}".format(filename), large) | |
large_outline = outline(cropped) | |
large_outline = fit_to_size(large_outline, (100, 100)) | |
cv2.imwrite("100x100_outline/{}".format(filename), large_outline) | |
small_outline = outline(small) | |
small_outline = fit_to_size(small_outline, (50, 50)) | |
cv2.imwrite("50x50_outline/{}".format(filename), small_outline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment