def _raw_canvas(self): # Create the sprite canvas width, height = self.sprite.canvas_size canvas = PILImage.new('RGBA', (width, height), (0, 0, 0, 0)) # Paste the images inside the canvas for image in self.sprite.images: canvas.paste( image.image, (round_up(image.x + (image.padding[3] + image.margin[3]) * self.sprite.max_ratio), round_up(image.y + (image.padding[0] + image.margin[0]) * self.sprite.max_ratio))) meta = PngImagePlugin.PngInfo() meta.add_text('Software', 'glue-%s' % __version__) meta.add_text('Comment', self.sprite.hash) # Customize how the png is going to be saved kwargs = dict(optimize=False, pnginfo=meta) if self.sprite.config['png8']: # Get the alpha band alpha = canvas.split()[-1] canvas = canvas.convert('RGB').convert('P', palette=PILImage.ADAPTIVE, colors=255) # Set all pixel values below 128 to 255, and the rest to 0 mask = PILImage.eval(alpha, lambda a: 255 if a <= 128 else 0) # Paste the color of index 255 and use alpha as a mask canvas.paste(255, mask) kwargs.update({'transparency': 255}) return canvas, kwargs
def _raw_canvas(self): # Create the sprite canvas width, height = self.sprite.canvas_size canvas = PILImage.new('RGBA', (width, height), (0, 0, 0, 0)) # Paste the images inside the canvas for image in self.sprite.images: canvas.paste(image.image, (round_up(image.x + (image.padding[3] + image.margin[3]) * self.sprite.max_ratio), round_up(image.y + (image.padding[0] + image.margin[0]) * self.sprite.max_ratio))) meta = PngImagePlugin.PngInfo() meta.add_text('Software', 'glue-%s' % __version__) meta.add_text('Comment', self.sprite.hash) # Customize how the png is going to be saved kwargs = dict(optimize=False, pnginfo=meta) if self.sprite.config['png8']: # Get the alpha band alpha = canvas.split()[-1] canvas = canvas.convert('RGB' ).convert('P', palette=PILImage.ADAPTIVE, colors=255) # Set all pixel values below 128 to 255, and the rest to 0 mask = PILImage.eval(alpha, lambda a: 255 if a <= 128 else 0) # Paste the color of index 255 and use alpha as a mask canvas.paste(255, mask) kwargs.update({'transparency': 255}) return canvas, kwargs
def canvas_size(self): """Return the width and height for this sprite canvas""" width = height = 0 for image in self.images: x = image.x + image.absolute_width y = image.y + image.absolute_height if width < x: width = x if height < y: height = y return round_up(width), round_up(height)
def save(self, ratio): width, height = self.sprite.canvas_size canvas, kwargs = self._raw_canvas # Loop all over the ratios and save one image for each one for ratio in self.sprite.config['ratios']: # Create the destination directory if required if not os.path.exists(self.output_dir(ratio=ratio)): os.makedirs(self.output_dir(ratio=ratio)) image_path = self.output_path(ratio=ratio) # If this canvas isn't the biggest one scale it using the ratio if self.sprite.max_ratio != ratio: reduced_canvas = canvas.resize( (round_up((width / self.sprite.max_ratio) * ratio), round_up((height / self.sprite.max_ratio) * ratio)), PILImage.ANTIALIAS) reduced_canvas.save(image_path, **kwargs) # TODO: Use Imagemagick if it's available else: canvas.save(image_path, **kwargs)
def save(self, ratio): width, height = self.sprite.canvas_size canvas, kwargs = self._raw_canvas # Loop all over the ratios and save one image for each one for ratio in self.sprite.config['ratios']: # Create the destination directory if required if not os.path.exists(self.output_dir(ratio=ratio)): os.makedirs(self.output_dir(ratio=ratio)) image_path = self.output_path(ratio=ratio) # If this canvas isn't the biggest one scale it using the ratio if self.sprite.max_ratio != ratio: reduced_canvas = canvas.resize( (round_up((width / self.sprite.max_ratio) * ratio), round_up((height / self.sprite.max_ratio) * ratio)), self.sprite.config['scaling']) reduced_canvas.save(image_path, **kwargs) # TODO: Use Imagemagick if it's available else: canvas.save(image_path, **kwargs)
def get_context(self, *args, **kwargs): sprite_path = os.path.relpath(self.sprite.sprite_path(), self.output_dir()) sprite_path = self.fix_windows_path(sprite_path) context = {'version': __version__, 'hash': self.sprite.hash, 'name': self.sprite.name, 'sprite_path': sprite_path, 'sprite_filename': os.path.basename(sprite_path), 'width': round_up(self.sprite.canvas_size[0] / self.sprite.max_ratio), 'height': round_up(self.sprite.canvas_size[1] / self.sprite.max_ratio), 'images': [], 'ratios': {}} for i, img in enumerate(self.sprite.images): base_x = img.x * -1 - img.margin[3] * self.sprite.max_ratio base_y = img.y * -1 - img.margin[0] * self.sprite.max_ratio base_abs_x = img.x + img.margin[3] * self.sprite.max_ratio base_abs_y = img.y + img.margin[0] * self.sprite.max_ratio image = dict(filename=img.filename, last=i == len(self.sprite.images) - 1, x=round_up(base_x / self.sprite.max_ratio), y=round_up(base_y / self.sprite.max_ratio), abs_x=round_up(base_abs_x / self.sprite.max_ratio), abs_y=round_up(base_abs_y / self.sprite.max_ratio), height=round_up((img.height / self.sprite.max_ratio) + img.padding[0] + img.padding[2]), width=round_up((img.width / self.sprite.max_ratio) + img.padding[1] + img.padding[3]), original_width=img.original_width, original_height=img.original_height, ratios={}) for r in self.sprite.ratios: image['ratios'][r] = dict(filename=img.filename, last=i == len(self.sprite.images) - 1, x=round_up(base_x / self.sprite.max_ratio * r), y=round_up(base_y / self.sprite.max_ratio * r), abs_x=round_up(base_abs_x / self.sprite.max_ratio * r), abs_y=round_up(base_abs_y / self.sprite.max_ratio * r), height=round_up((img.height + img.padding[0] + img.padding[2]) / self.sprite.max_ratio * r), width=round_up((img.width + img.padding[1] + img.padding[3]) / self.sprite.max_ratio * r)) context['images'].append(image) # Ratios for r in self.sprite.ratios: ratio_sprite_path = os.path.relpath(self.sprite.sprite_path(ratio=r), self.output_dir()) ratio_sprite_path = self.fix_windows_path(ratio_sprite_path) context['ratios'][r] = dict(ratio=r, fraction=nearest_fration(r), sprite_path=ratio_sprite_path, sprite_filename=os.path.basename(ratio_sprite_path), width=round_up(self.sprite.canvas_size[0] / self.sprite.max_ratio * r), height=round_up(self.sprite.canvas_size[1] / self.sprite.max_ratio * r)) return context
def absolute_height(self): """Return the total height of the image taking count of the margin, padding and ratio. """ return round_up(self.height + self.vertical_spacing * max(self.config['ratios']))
def absolute_width(self): """Return the total width of the image taking count of the margin, padding and ratio.""" return round_up(self.width + self.horizontal_spacing * max(self.config['ratios']))
def scale_down(self, value, ratio): return round_up(value / ratio)
def absolute_height(self): """Return the total height of the image taking count of the margin, padding and ratio. """ return round_up(self.height + self.vertical_spacing * max(self.config["ratios"]))
def absolute_width(self): """Return the total width of the image taking count of the margin, padding and ratio.""" return round_up(self.width + self.horizontal_spacing * max(self.config["ratios"]))