Seite 1 von 1

Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 21:25
von maxi_king_333
Hallo,

ich habe vor mir ein kleines Info-Display zu schreiben, welches mir Wetter und ein paar andere Dinge anzeigen soll.
Das Interface wollte ich in Pygame gestalten, ich brauche keine Userinteraktion und 2 Frames pro Sekunde reichen auch.
Beim Versuch auf ein Hintergrundbild eine weiße leicht transparente Uhrzeit zu zeichnen, bin ich jedoch gescheitert.

Code: Alles auswählen

import os.path
import time

import pygame

pygame.init()
pygame.display.set_caption('Infoframe')

screen = pygame.display.set_mode((1024, 768))
background = pygame.transform.smoothscale(pygame.image.load(os.path.join('data', 'background.jpg')), (screen.get_width(), screen.get_height())).convert()
font = pygame.font.Font(pygame.font.get_default_font(), 90)
text = font.render(time.strftime('%H : %M : %S'), True, (255, 255, 255))
text.set_alpha(100)

screen.blit(background, (0, 0))
screen.blit(text, (0, 0))

pygame.display.flip()

time.sleep(5)
Die Uhrzeit ist einfach nicht transparent.
Die Pygame Dokumentation sagt dazu:
Set the current alpha value fo r the Surface. When blitting this Surface onto a destination, the pixels will be drawn slightly transparent. The alpha value is an integer from 0 to 255, 0 is fully transparent and 255 is fully opaque. If None is passed for the alpha value, then the Surface alpha will be disabled.

This value is different than the per pixel Surface alpha. If the Surface format contains per pixel alphas, then this alpha value will be ignored. If the Surface contains per pixel alphas, setting the alpha value to None will disable the per pixel transparency.
Ich denke also, das ich diese pro Pixel Transparenz habe.
Wenn ich den Text mit convert() oder convert_alpha() konvertiere, dann bekomme ich einen transparenten weißen Balken.
Es muss doch irgendwie möglich sein einen Text transparent zu machen, oder?

Als Hintergrundbild verwende ich folgendes:
http://gnome-look.org/content/show.php/ ... tent=68175

Viele Grüße und vielen Dank
Maxi

Re: Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 21:39
von /me
maxi_king_333 hat geschrieben:Es muss doch irgendwie möglich sein einen Text transparent zu machen, oder?
Unter http://nedbatchelder.com/blog/200801/tr ... h_pil.html gibt es ein Rezept dafür.

Re: Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 21:47
von maxi_king_333
Danke, aber das ist doch für PIL und jede Sekunde ein Bild mit PIL zeichnen, abspeichern und mit Pygame anzeigen, ist das nicht ein bisschen ineffizient.

Re: Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 22:16
von /me
maxi_king_333 hat geschrieben:Danke, aber das ist doch für PIL und jede Sekunde ein Bild mit PIL zeichnen, abspeichern und mit Pygame anzeigen, ist das nicht ein bisschen ineffizient.
Ach Mist, irgendwie hatte ich bei dem Link Pygame im Kopf.

Re: Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 22:20
von yipyip
Musste auch erst guugeln und probieren. Man braucht noch ein 2. Surface. Auf dem setzt man dann den alpha Wert.

Code: Alles auswählen

import os.path
import time

import pygame

pygame.init()
pygame.display.set_caption('Infoframe')

screen = pygame.display.set_mode((1024, 768))
background = pygame.transform.smoothscale(pygame.image.load('background.jpg'),
                                          (screen.get_width(), screen.get_height()))
background = background.convert_alpha()

font = pygame.font.Font(pygame.font.get_default_font(), 90)
text = time.strftime('%H : %M : %S')

bg = (0, 0, 0)
font_surface = font.render(text, True, (255, 255, 255), bg)
surf = pygame.Surface(font.size(text))
surf.set_alpha(77)
surf.set_colorkey(bg)
surf.blit(font_surface, (0, 0))

screen.blit(background, (0, 0))
screen.blit(surf, (110, 110))
pygame.display.flip()

time.sleep(3)
:wink:
yipyip

Re: Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 22:47
von maxi_king_333
Vielen Dank, das sieht doch schon sehr gut aus, jedoch werden jetzt um den Text so kleine schwarze Ränder gezeichnet.
Wenn die jetzt auch noch weg gingen, dann wäre es perfekt. :D

Re: Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 23:44
von BlackJack
Ursprüngliches Beispiel und da nach dem ``text =...`` folgendes hinzufügen:

Code: Alles auswählen

alpha = pygame.surfarray.pixels_alpha(text)
alpha *= 0.5
del alpha
Das mit dem ``del`` ist megahässlich aber die Pygame-Entwickler haben das so implementiert, dass das `alpha`-Objekt das `Surface` sperrt solange es existiert. Das ist eine dermassen dumme API das die Entwickler dafür verhauen gehören. :evil:

Re: Pygame: Transparente Font

Verfasst: Dienstag 1. November 2011, 23:59
von yipyip
Ah, danke, da stand ich jetzt ziemlich auf dem Schlauch.

:wink:
yipyip

Re: Pygame: Transparente Font

Verfasst: Mittwoch 2. November 2011, 14:08
von maxi_king_333
@BlackJack: Vielen Dank, genau so habe ich mir das vorgestellt.