[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] pygame and cairo
Hi,
I put up a cookbook entry on how to alias a cairo surface
to a pygame surface (and a numpy array):
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498278
This is really cool :)
Simon.
#!/usr/bin/env python
import cairo
w, h = 128, 128
# Setup Cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx = cairo.Context(surface)
# Set thickness of brush
ctx.set_line_width(15)
# Draw out the triangle using absolute coordinates
ctx.move_to(w/2, h/3)
ctx.line_to(2*w/3, 2*h/3)
ctx.rel_line_to(-1*w/3, 0)
ctx.close_path()
# Apply the ink
ctx.stroke()
# Output a PNG file
surface.write_to_png("triangle.png")
# Alias the image as a numpy array
import numpy
# This needs better than pycairo-1.2.2, eg. pycairo CVS:
# cvs -d :pserver:anoncvs@xxxxxxxxxxxxxxxxxxx:/cvs/cairo co pycairo
buf = surface.get_data()
a = numpy.frombuffer(buf, numpy.uint8)
a.shape = (w, h, 4)
a[:,:,2] = 255
surface.write_to_png("triangle1.png") # red triangle..
# Alias the image as a pygame surface
import pygame
from time import sleep
imsurf = pygame.image.frombuffer(buf, (w,h), "RGBA")
depth = 4*8
pygame.display.init()
surface = pygame.display.set_mode((w,h), pygame.DOUBLEBUF, depth)
done = False
while not done:
surface.blit(imsurf, (0,0)) # blue triangle..
sleep(0.1)
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
done = True
pygame.display.flip()