Created
November 2, 2015 19:52
-
-
Save Painted-Fox/ba697556cbb1251a5cf4 to your computer and use it in GitHub Desktop.
Basic script for splitting a sprite image up.
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
"""Basic script for splitting a sprite image up.""" | |
import os | |
import os.path | |
from PIL import Image | |
from PIL import ImageDraw | |
ICON_SIZE = 64 | |
MINI_ICON_SIZE = 20 | |
HORIZ_WIDTH = 50 | |
HORIZ_HEIGHT = 36 | |
HORIZ_SIZE = HORIZ_WIDTH, HORIZ_HEIGHT | |
FORK_WIDTH = 80 | |
FORK_HEIGHT = 100 | |
FORK_SIZE = FORK_WIDTH, FORK_HEIGHT | |
LINE_COLOR = '#969696' | |
ICONS = { | |
'accessedfromhistory': (0, 271), | |
'research': (0, 143), | |
'entitysearchfilter': (0, 975), | |
'removefilter': (0, 911), | |
'entitysearch': (0, 1870), | |
'profilesuitereportview': (0, 1936), | |
'contentfilter': (0, 207), | |
'accessedfromhistory': (0, 271), | |
'unspecified': (0, 335), | |
'alert': (0, 399), | |
'findsimilardocuments': (0, 463), | |
'purchaseddocument': (0, 527), | |
'print': (0, 591), | |
'download': (0, 655), | |
'email': (0, 719), | |
'folderdocview': (0, 783), | |
'comparesearches': (0, 847), | |
'tocview': (0, 1103), | |
'topicsummary': (0, 1167), | |
'lpa-lnpg_browse': (0, 1306), | |
'documentview-webdocumentview': (0, 2003), | |
'updatefolderitem': (3, 1235), | |
} | |
MINI_ICONS = { | |
'shepards': (96, 24), | |
'vsasearch': (50, 0), | |
'publicrecord': (72, 24), | |
'medicallitigatorsearch': (26, 1610), | |
'lpasearch': (50, 10), | |
'topicsearch': (98, 1610), | |
'lmosearch': (2, 33), | |
'profilesuitesearch': (48, 1633), | |
'sourcesearch': (100, 1658), | |
'taxsearch': (2, 1681), | |
'casebasesearch': (50, 1681), | |
'quickcitesearch': (104, 1681), | |
'quickcitesearch': (104, 1681), | |
} | |
COMPOSITES = { | |
'lpasearch': ('research', 'lpasearch'), | |
'medicallitigatorsearch': ('research', 'medicallitigatorsearch'), | |
'profilesuitesearch': ('research', 'profilesuitesearch'), | |
'publicrecordsearch': ('research', 'publicrecord'), | |
'removeshepardsfilter': ('removefilter', 'shepards'), | |
'shepardsearch': ('research', 'shepards'), | |
'sourcesearch': ('research', 'sourcesearch'), | |
'taxsearch': ('research', 'taxsearch'), | |
'topicsearch': ('research', 'topicsearch'), | |
'vsasearch': ('research', 'vsasearch'), | |
} | |
def get_icon(sprite, size, left, top): | |
"""Gets an icon out of a sprite image, given the position and size. | |
Assumes a square icon.""" | |
return sprite.crop((left, top, left + size, top + size)) | |
def get_mini(sprite, container, size, left, top): | |
"""Retrieves the mini icon out of the sprite and places it into the | |
container image.""" | |
img = container.copy() | |
mini = get_icon(sprite, size, left, top) | |
img.paste(mini, (1, 1), mini) | |
return img | |
def lengthen(image, pxleft, pxright): | |
"""Lengthens an image by given number of pixels to the left and right.""" | |
size = (image.size[0] + pxleft + pxright, image.size[1]) | |
result = Image.new('RGBA', size) | |
result.paste(image, (pxleft, 0), image) | |
return result | |
def process(sprite, path): | |
"""Go through our list of icons. Only save an icon if it doesn't exist.""" | |
for name, position in ICONS.items(): | |
left = position[0] | |
top = position[1] | |
file_name = name + '.png' | |
file_path = os.path.join(path, file_name) | |
if not os.path.exists(file_path): | |
icon = get_icon(sprite, ICON_SIZE, left, top) | |
icon = lengthen(icon, 11, 11) | |
icon.save(file_path) | |
def process_composites(sprite, container, path): | |
"""Put together the images that are composites of the large icons and mini | |
icons.""" | |
for name, combo in COMPOSITES.items(): | |
file_name = name + '.png' | |
file_path = os.path.join(path, file_name) | |
if not os.path.exists(file_path): | |
icon_pos = ICONS[combo[0]] | |
mini_pos = MINI_ICONS[combo[1]] | |
icon = get_icon(sprite, ICON_SIZE, *icon_pos) | |
icon = lengthen(icon, 11, 11) | |
mini = get_mini(sprite, container, MINI_ICON_SIZE, *mini_pos) | |
icon.paste(mini, (0, 21), mini) | |
icon.save(file_path) | |
def draw_horiz(path): | |
"""Draw the horizontal line that connects two elements.""" | |
im = Image.new('LA', HORIZ_SIZE) | |
draw = ImageDraw.Draw(im) | |
linexy = [(0, 1), (HORIZ_WIDTH, 1)] | |
draw.line(linexy, fill=LINE_COLOR, width=1) | |
del draw | |
im.save(os.path.join(path, 'horizontal.png')) | |
def draw_fork(path): | |
"""Draw the fork line that represents a branch.""" | |
im = Image.new('LA', FORK_SIZE) | |
draw = ImageDraw.Draw(im) | |
line1xy = [(FORK_WIDTH / 2, 0), (FORK_WIDTH / 2, FORK_HEIGHT - HORIZ_HEIGHT)] | |
line2xy = [(FORK_WIDTH / 2, FORK_HEIGHT - HORIZ_HEIGHT + 1), (FORK_WIDTH, FORK_HEIGHT - HORIZ_HEIGHT + 1)] | |
draw.line(line1xy, fill=LINE_COLOR, width=1) | |
draw.line(line2xy, fill=LINE_COLOR, width=1) | |
del draw | |
im.save(os.path.join(path, 'fork.png')) | |
if __name__ == '__main__': | |
#sprite = Image.open(os.path.join('images', 'rm-iconset.png')) | |
#container = Image.open(os.path.join('images', 'mini-icon-container.png')) | |
#process(sprite, 'icons') | |
#process_composites(sprite, container, 'icons') | |
draw_horiz('lines') | |
draw_fork('lines') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment