atlas: automatically generate a border of 1px for each image in the atlas. refs #289

This commit is contained in:
Mathieu Virbel 2012-10-23 13:06:44 +02:00
parent 29bae666e2
commit 831ef30523
3 changed files with 23 additions and 9 deletions

View File

@ -192,7 +192,7 @@ class Atlas(EventDispatcher):
self.textures = textures
@staticmethod
def create(outname, filenames, size, padding=1):
def create(outname, filenames, size, padding=2):
'''This method can be used to create manually an atlas from a set of
images.
@ -204,11 +204,17 @@ class Atlas(EventDispatcher):
List of filename to put in the atlas
`size`: int
Size of an atlas image
`padding`: int, default to 1
Padding to put around each image. Care, if you put 0, they might
be some issues with OpenGL, because by default, Kivy texture are
using GL_CLAMP_TO_EDGE, and the edge is another image than
the image you'll want to display.
`padding`: int, default to 2
Padding to put around each image.
Be careful. If you're using a padding < 2, you might get issues
with border of the images. Because of the OpenGL linearization,
it might take the pixels of the adjacent image.
If you're using a padding >= 2, we'll automatically generate a
"border" of 1px of your image, around the image. If you look at
the result, don't be scared if the image inside it are not
exactly the same as yours :).
'''
# Thanks to
# omnisaurusgames.com/2011/06/texture-atlas-generation-using-python/
@ -232,7 +238,6 @@ class Atlas(EventDispatcher):
# the freebox tuple format is: outidx, x, y, w, h
freeboxes = [(0, 0, 0, size, size)]
numoutimages = 1
padding = 1
# full boxes are areas where we have placed images in the atlas
# the full box tuple format is: image, outidx, x, y, w, h, filename
@ -291,7 +296,16 @@ class Atlas(EventDispatcher):
outimages = [Image.new('RGBA', (size, size))
for i in range(0, int(numoutimages))]
for fb in fullboxes:
outimages[fb[1]].paste(fb[0], (fb[2], fb[3]))
x, y = fb[2], fb[3]
out = outimages[fb[1]]
out.paste(fb[0], (fb[2], fb[3]))
w, h = fb[0].size
if padding > 1:
out.paste(fb[0].crop((0, 0, w, 1)), (x, y - 1))
out.paste(fb[0].crop((0, h - 1, w, h)), (x, y + h))
out.paste(fb[0].crop((0, 0, 1, h)), (x - 1, y))
out.paste(fb[0].crop((w - 1, 0, w, h)), (x + w, y))
# save the output images
for idx, outimage in enumerate(outimages):

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 43 KiB

View File

@ -1 +1 @@
{"defaulttheme-0.png": {"player-play-overlay": [306, 396, 117, 115], "spinner_pressed": [91, 124, 29, 37], "progressbar_background": [220, 137, 24, 24], "media-playback-pause": [448, 344, 48, 48], "tab_btn_pressed": [465, 178, 32, 32], "image-missing": [399, 344, 48, 48], "filechooser_selected": [187, 393, 118, 118], "audio-volume-muted": [350, 344, 48, 48], "sliderv_background": [473, 404, 37, 41], "tab": [332, 228, 96, 32], "close": [490, 491, 20, 20], "ring": [1, 326, 185, 185], "vkeyboard_key_down": [121, 129, 32, 32], "vkeyboard_background": [187, 328, 64, 64], "checkbox_off": [300, 178, 32, 32], "bubble_arrow": [148, 162, 16, 10], "player-background": [228, 222, 103, 103], "bubble": [424, 446, 65, 65], "spinner": [61, 124, 29, 37], "sliderh_background": [148, 173, 41, 37], "audio-volume-medium": [301, 344, 48, 48], "media-playback-start": [1, 162, 48, 48], "tab_btn": [432, 178, 32, 32], "bubble_btn_pressed": [267, 178, 32, 32], "tree_closed": [490, 470, 20, 20], "switch-background": [429, 228, 83, 32], "filechooser_file": [332, 261, 64, 64], "checkbox_radio_off": [366, 178, 32, 32], "vkeyboard_key_normal": [154, 129, 32, 32], "checkbox_radio_on": [399, 178, 32, 32], "checkbox_on": [333, 178, 32, 32], "tree_opened": [490, 449, 20, 20], "button_pressed": [31, 124, 29, 37], "media-playback-stop": [50, 162, 48, 48], "audio-volume-high": [424, 397, 48, 48], "audio-volume-low": [252, 344, 48, 48], "bubble_btn": [234, 178, 32, 32], "modalview-background": [462, 271, 45, 54], "button": [1, 124, 29, 37], "progressbar": [187, 137, 32, 24], "switch-button": [190, 178, 43, 32], "filechooser_folder": [397, 261, 64, 64], "slider_cursor": [99, 162, 48, 48], "textinput_active": [1, 211, 114, 114], "textinput": [116, 214, 111, 111]}}
{"defaulttheme-0.png": {"player-play-overlay": [309, 395, 117, 115], "spinner_pressed": [384, 170, 29, 37], "progressbar_background": [483, 183, 24, 24], "media-playback-pause": [52, 159, 48, 48], "tab_btn_pressed": [138, 125, 32, 32], "image-missing": [2, 159, 48, 48], "filechooser_selected": [189, 392, 118, 118], "audio-volume-muted": [452, 342, 48, 48], "sliderv_background": [252, 166, 37, 41], "tab": [336, 225, 96, 32], "close": [240, 137, 20, 20], "ring": [2, 325, 185, 185], "vkeyboard_key_down": [172, 125, 32, 32], "vkeyboard_background": [189, 326, 64, 64], "checkbox_off": [449, 175, 32, 32], "bubble_arrow": [495, 500, 16, 10], "player-background": [231, 220, 103, 103], "bubble": [428, 445, 65, 65], "spinner": [353, 170, 29, 37], "sliderh_background": [468, 286, 41, 37], "audio-volume-medium": [402, 342, 48, 48], "media-playback-start": [102, 159, 48, 48], "tab_btn": [104, 125, 32, 32], "bubble_btn_pressed": [415, 175, 32, 32], "tree_closed": [262, 137, 20, 20], "switch-background": [428, 411, 83, 32], "filechooser_file": [336, 259, 64, 64], "checkbox_radio_off": [36, 125, 32, 32], "vkeyboard_key_normal": [206, 125, 32, 32], "checkbox_radio_on": [70, 125, 32, 32], "checkbox_on": [2, 125, 32, 32], "tree_opened": [284, 137, 20, 20], "button_pressed": [322, 170, 29, 37], "media-playback-stop": [152, 159, 48, 48], "audio-volume-high": [302, 342, 48, 48], "audio-volume-low": [352, 342, 48, 48], "bubble_btn": [479, 225, 32, 32], "modalview-background": [255, 336, 45, 54], "button": [291, 170, 29, 37], "progressbar": [468, 260, 32, 24], "switch-button": [434, 225, 43, 32], "filechooser_folder": [402, 259, 64, 64], "slider_cursor": [202, 159, 48, 48], "textinput_active": [2, 209, 114, 114], "textinput": [118, 212, 111, 111]}}