make_frame
Diese Funktion skaliert ein Bild ohne Größenänderung der Ecken auf eine beliebige Größe. Dazu wird jeweils die mittlere Pixelreihe in horizontaler und vertikaler Richtung in die Länge bzw. Breite "gezogen".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | def make_frame(img, width, height):
"""
Teilt die übergebene Surface img in 9 Teile.
| |
----------------
| |
| |
----------------
| |
Zurückgegeben wird ein dict mit allen Teilen, über
folgende keys erreichbar: n, ne, e, se, s, sw, w, nw, c
"""
w, h = img.get_size()
half_x = int(w/2+1)
half_y = int(h/2+1)
parts = []
widths = ((0,half_x),(half_x, half_x+1), (half_x+1,w))
heights= ((0,half_y),(half_y, half_y+1), (half_y+1,h))
for x1, x2 in widths:
for y1, y2 in heights:
s = pygame.surface.Surface((x2-x1, y2-y1)).convert_alpha()
s.blit(img, (0,0), pygame.rect.Rect((x1,y1,x2,y2)))
parts.append(s)
direction_mapping = ["nw", "w", "sw", "n", "c", "s", "ne", "e", "se"]
parts = dict(zip(direction_mapping, parts))
resize_width = width - w + 1
resize_height = height - h + 1
# height resizing
for k in "wce":
parts[k] = pygame.transform.scale(parts[k], (parts[k].get_width(), resize_height))
# width resizing
for k in "ncs":
parts[k] = pygame.transform.scale(parts[k], (resize_width, parts[k].get_height()))
# frame it
s = pygame.surface.Surface((width, height)).convert_alpha()
x, y = 0,0
for yk in ["n", "", "s"]:
for xk in ["w", "", "e"]:
k=yk+xk or "c"
s.blit(parts[k],(x,y))
x+=parts[k].get_width()
y+=parts[k].get_height()
x=0
return s
|
erstellt am 14.5.2008 21:43, zuletzt gendert am 20.12.2008 10:44
