Path: blob/master/src/engine/icongridutils.py
254 views
from PIL import Image12def _icongrid_add_row(icongrid, iconsize=150):3icongrid_cpy = icongrid.copy()4new_icongrid = Image.new('RGBA', (icongrid_cpy.size[0], icongrid_cpy.size[1]+iconsize), (0, 0, 0, 0))5new_icongrid.paste(icongrid_cpy)6icongrid_cpy.close()7return new_icongrid89def _get_last_row_and_col(icongrid, iconsize=150):10_box = icongrid.getbbox()11pix_to_index = lambda pixels: pixels//iconsize1213if _box:14# finding last row index15_, _, _, lastrow_y = _box16last_row_index = pix_to_index(lastrow_y)1718# finding last col index19_last_row_img = icongrid.crop((0, last_row_index*iconsize, icongrid.width, last_row_index*iconsize + iconsize))2021_box = _last_row_img.getbbox()22if _box:23_, _, lastcol_x, _ = _box24else:25print("ERROR: error in trying to find last column, setting to 0")26lastcol_x = 027last_col_index = pix_to_index(lastcol_x)28else:29print("ERROR: icongrid is empty, cannot find bbox")30last_row_index = 031last_col_index = 03233return last_row_index, last_col_index3435ICON_PERFECT_FIT = 036ICON_BIGGER_THAN_AREA = 137ICON_SMALLER_THAN_AREA = 23839def _check_icon_size(icon, check_width=150, check_height=150):40# 0 = icon is 150x15041# 1 = icon is too wide/tall42# 2 = icon is smaller than 150x150 area43if icon.width == check_width and icon.height == check_height:44return ICON_PERFECT_FIT45elif icon.width > 150 or icon.height > 150:46return ICON_BIGGER_THAN_AREA47else:48return ICON_SMALLER_THAN_AREA4950def _center_icon(icon):51w, h = icon.size52final_icon = Image.new('RGBA', (150, 150), (0, 0, 0, 0))53dx = (150 - w) // 254dy = (150 - h) // 255final_icon.paste(icon, (dx, dy))56return final_icon5758def appendIconToGrid(icongrid_path, iconpaths, iconsize=150):59print("Icongrid from: {} \nIcons: {}".format(icongrid_path, len(iconpaths)))6061return_status = 062indices = []63problem_icon = None64exception_msg = None6566IMAGES_PER_COLUMN = 106768try:69# icongrid = Image.open(icongrid_path)70with Image.open(icongrid_path).convert('RGBA') as icongrid:71# icongrid = icongrid.convert('RGBA')72for iconpath in iconpaths:73# icon_img = Image.open(iconpath)74with Image.open(iconpath).convert('RGBA') as icon_img:75# check if icon_img is 150x15076can_fit = _check_icon_size(icon_img)77if can_fit == ICON_BIGGER_THAN_AREA:78# if the icon is too big, ignore it (for now)79return_status = 280problem_icon = iconpath81continue82elif can_fit == ICON_SMALLER_THAN_AREA:83print(f"Icon: {iconpath} is smaller than 150x150, centering it....")84icon_img = _center_icon(icon_img)8586# get location to paste it87last_row_idx, last_col_idx = _get_last_row_and_col(icongrid)8889new_index = last_row_idx*IMAGES_PER_COLUMN + last_col_idx + 190indices.append(new_index)91new_row_idx = new_index // IMAGES_PER_COLUMN92new_col_idx = new_index % IMAGES_PER_COLUMN9394if new_row_idx * iconsize >= icongrid.height:95print("Icongrid is full. Expanding it....")96icongrid = _icongrid_add_row(icongrid)9798icongrid.paste(icon_img, (new_col_idx*iconsize, new_row_idx*iconsize))99icongrid.save(icongrid_path)100except Exception as e:101return_status = -1102exception_msg = f"{e.__class__.__name__} : {str(e)}"103104105return return_status, indices, problem_icon, exception_msg106107def makePsychEngineIconGrid(iconpaths, savepath, img_size=150):108# this function works for any number of icons that you provide, even though psych engine uses 2 icons for a character109status = 0110problemimg = None111exception_msg = None112113good_icons = []114for iconpath in iconpaths:115try:116icon = Image.open(iconpath).convert('RGBA')117except Exception as e:118exception_msg = f"{e.__class__.__name__} : {str(e)}"119continue120fit_status = _check_icon_size(icon)121if fit_status == ICON_BIGGER_THAN_AREA:122problemimg = iconpath123status = 1124icon.close()125continue126elif fit_status == ICON_SMALLER_THAN_AREA:127print(f"Icon: {iconpath} is smaller than 150x150, centering it....")128icon = _center_icon(icon)129good_icons.append(icon)130131if len(good_icons) == 0:132return 1, problemimg, exception_msg133134final_icongrid = Image.new('RGBA', (img_size*len(good_icons), img_size), (0, 0, 0, 0))135for i, icon in enumerate(good_icons):136final_icongrid.paste(icon, (i*img_size, 0))137icon.close()138139final_icongrid.save(savepath)140final_icongrid.close()141142return status, problemimg, exception_msg143144