Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- BLANK = (255, 255, 255)
- class Sprite:
- """Represents a single icon on the sprite sheet. Has a width, height,
- left offset, right offset, and top offset."""
- def __init__(self, top):
- self.top = top
- self.width = 0
- self.height = 0
- # We update the offsets when the row's offset is smaller, so start
- # them impossibly high
- self.left = float('inf')
- self.right = float('inf')
- def add_row(self, row):
- """Add an ImageRow to this sprite, updating offsets and dimensions."""
- # The left offset of a sprite is the minimal left offset of all its
- # rows. Same goes for right. So, take the min of our current offset
- # and the row's offset so that, by the end, we'll have each offset
- # as the min of the corresponding offsets in each row.
- self.left = min(self.left, row.significant_left)
- self.right = min(self.right, row.significant_right)
- # Update our dimensions while we're here, too.
- self.width = (row.width - self.right) - self.left
- self.height += 1
- class ImageRow:
- """Represents a single row of pixels in the image. Has a width,
- top offset, significant left offset (where the non-blank pixels start, if
- there are any), and significant right offset."""
- def __init__(self, pix, width, y):
- self._pix = pix
- self.width = width
- self.y = y
- left_to_right = xrange(self.width) # 0 ... (width - 1)
- right_to_left = reversed(left_to_right) # (width - 1) ... 0
- # Find left offset and right offset of this row's significant section
- self.significant_left = self._significant_offset(left_to_right)
- self.significant_right = self._significant_offset(right_to_left)
- def is_significant(self):
- """Is this row significant (non-blank)? True or False."""
- return self.significant_left != None
- def _significant_offset(self, seq):
- """Given a sequence of x-offsets, return the first (if any) whose
- corresponding pixel is significant (non-blank)."""
- for offset, x in enumerate(seq):
- if self._pix[x, self.y] != BLANK:
- return offset
- return None
- # Open up the image, convert to RGB if it's not RGB yet, find dimensions
- sheet = Image.open("pkmn_icons.png").convert("RGB")
- width, height = sheet.size
- # Load the pixel accessor object (reads from here are faster than
- # sheet.getpixel)
- pix = sheet.load()
- # Set up our list of sprites to populate
- sprites = []
- # Loop through the rows of the image, keeping track of if we're working on
- # a sprite right now or not
- current_sprite = None
- for y in xrange(height):
- row = ImageRow(pix, width, y)
- if row.is_significant(): # row is non-blank
- # If there is no current sprite, this is the first row of a new
- # sprite. Set that sucker up.
- if not current_sprite:
- current_sprite = Sprite(y)
- sprites.append(current_sprite)
- # Add this row to the current sprite, which will update its width,
- # height, and offsets accordingly.
- current_sprite.add_row(row)
- elif current_sprite: # row is blank and there is a current sprite
- # This is the first blank row after a sprite. The sprite is complete.
- current_sprite = None
- # Print the CSS line for each sprite
- for i, sprite in enumerate(sprites):
- selector = ".flair-{0}::before".format(i + 1)
- rules = []
- rules.append("width:{0}px".format(sprite.width))
- rules.append("height:{0}px".format(sprite.height))
- rules.append("background-position:{0}px {1}px".format(-sprite.left, -sprite.top))
- print selector + "{" + ";".join(rules) + "}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement