def make_bundle(self, versioner): import Image # If this fails, you need the Python Imaging Library. boxes = [ImageBox(Image.open(path), path) for path in self.get_paths()] # Pick a max_width so that the sprite is squarish and a multiple of 16, # and so no image is too wide to fit. total_area = sum(box.width * box.height for box in boxes) width = max(max(box.width for box in boxes), (int(math.sqrt(total_area)) // 16 + 1) * 16) (_, height, packing) = pack_boxes(boxes, width) sprite = Image.new("RGBA", (width, height)) for (left, top, box) in packing: # This is a bit of magic to make the transparencies work. To # preserve transparency, we pass the image so it can take its # alpha channel mask or something. However, if the image has no # alpha channels, then it fails, we we have to check if the # image is RGBA here. img = box.image mask = img if img.mode == "RGBA" else None sprite.paste(img, (left, top), mask) sprite.save(self.get_bundle_path(), "PNG") self._optimize_output() # It's *REALLY* important that this happen here instead of after the # generate_css() call, because if we waited, the CSS woudl have the URL # of the last version of this bundle. if versioner: versioner.update_bundle_version(self) self.generate_css(packing)
def make_bundle(self): import Image # If this fails, you need the Python Imaging Library. boxes = [ImageBox(Image.open(path), path) for path in self.get_paths()] # Pick a max_width so that the sprite is squarish and a multiple of 16, # and so no image is too wide to fit. total_area = sum(box.width * box.height for box in boxes) width = max(max(box.width for box in boxes), (int(math.sqrt(total_area)) // 16 + 1) * 16) (_, height, packing) = pack_boxes(boxes, width) sprite = Image.new("RGBA", (width, height)) with open(self.css_file, "w") as css: css.write("/* Generated classes for django-media-bundler sprites. " "Don't edit! */\n") props = { "background-image": "url('%s')" % self.get_bundle_url(), } css.write(self.make_css(None, props)) for (left, top, box) in packing: # This is a bit of magic to make the transparencies work. To # preserve transparency, we pass the image so it can take its # alpha channel mask or something. However, if the image has no # alpha channels, then it fails, we we have to check if the # image is RGBA here. img = box.image mask = img if img.mode == "RGBA" else None sprite.paste(img, (left, top), mask) props = { "background-position": "%dpx %dpx" % (-left, -top), "width": "%dpx" % box.width, "height": "%dpx" % box.height, } css.write(self.make_css(os.path.basename(box.filename), props)) sprite.save(self.get_bundle_path(), "PNG") self._optimize_output()
def make_bundle(self): import Image # If this fails, you need the Python Imaging Library. boxes = [ImageBox(Image.open(path), path) for path in self.get_paths()] # Pick a max_width so that the sprite is squarish and a multiple of 16, # and so no image is too wide to fit. total_area = sum(box.width * box.height for box in boxes) width = max(max(box.width for box in boxes), (int(math.sqrt(total_area)) // 16 + 1) * 16) (_, height, packing) = pack_boxes(boxes, width) sprite = Image.new("RGBA", (width, height)) with open(self.css_file, "w") as css: css.write( "/* Generated classes for django-media-bundler sprites. " "Don't edit! */\n") props = { "background-image": "url('%s')" % self.get_bundle_url(), } css.write(self.make_css(None, props)) for (left, top, box) in packing: # This is a bit of magic to make the transparencies work. To # preserve transparency, we pass the image so it can take its # alpha channel mask or something. However, if the image has no # alpha channels, then it fails, we we have to check if the # image is RGBA here. img = box.image mask = img if img.mode == "RGBA" else None sprite.paste(img, (left, top), mask) props = { "background-position": "%dpx %dpx" % (-left, -top), "width": "%dpx" % box.width, "height": "%dpx" % box.height, } css.write(self.make_css(os.path.basename(box.filename), props)) sprite.save(self.get_bundle_path(), "PNG") self._optimize_output()