[cairo] Pycairo + PNG Output

James Mills prologic at shortcircuit.net.au
Tue Oct 11 11:07:51 PDT 2005


On Tue, Oct 11, 2005 at 09:56:46AM -0400, Owen Taylor wrote:
> In C, you can do this with cairo_surface_write_to_png_stream() ...
> don't know if/how this is bound in the Python bindings. I'd think the
> natural mapping would be something like:
> 
>  buffer = StringIO.StringIO()
>  surface.write_to_png_stream(buffer)
>  pngData = buffer.getvalue()
>  buffer.close()

I'm afraid the binding doesn't exist.

I posted earlier an email about this relating to the API.

$ ./ex1.py 
Traceback (most recent call last):
  File "./ex1.py", line 57, in ?
    main()
  File "./ex1.py", line 42, in main
    surface.write_to_png_stream(buffer)
AttributeError: 'cairo.ImageSurface' object has no attribute
'write_to_png_stream'

$ cat ex1.py
#!/usr/bin/env python

import cairo
import pygame
import StringIO
from pygame.locals import *

WIDTH = 400
HEIGHT = 400

def main():

	pygame.init()
	screen = pygame.display.set_mode((640,480))
	pygame.display.set_caption("PyCairo Example")
	screen.fill((255, 255, 255, 255))
	pygame.display.update()

	pygame.mouse.set_visible(1)

	surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
	ctx = cairo.Context(surface)

	ctx.set_line_width(1)

	ctx.move_to(200, 100)
	ctx.line_to(300, 300)
	ctx.rel_line_to(-200, 0)
	ctx.close_path()

	ctx.stroke()
	#surface.write_to_png("triangle.png")

	#image = pygame.image.load(surface.write_to_png())
	#import tempfile
	#f = tempfile.NamedTemporaryFile()
	#surface.write_to_png(f.name)
	#image = pygame.image.load(f.name)
	#f.close()

	buffer = StringIO.StringIO()
	surface.write_to_png_stream(buffer)
	#pngData = buffer.getvalue()
	image = pygame.image.frombuffer(buffer)
	buffer.close()

	screen.blit(image, [0, 0])
	pygame.display.update()

	done = False
	while not done:
		for event in pygame.event.get():
			if event.type is QUIT:
				done = True

if __name__ == "__main__":
	main()
 
We (I) pygame/python developers would like to do something like the
aboave in our programs...

cheers
James

-- 
--
-"Problems are Solved by Method"
-
- James Mills <prologic at shortcircuit.net.au>
- HomePage: http://shortcircuit.net.au/~prologic/
- Phone: +61732166379
- Mobile: +61404270962
- Skype: therealprologic
- MSN: prologic at shortcircuit.net.au
- ICQ: 98888663
- IRC: irc://shortcircuit.net.au#se

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html


More information about the cairo mailing list