[cairo] image scaling in cairo, python

Gerdus van Zyl gerdusvanzyl at gmail.com
Sat May 24 04:05:20 PDT 2008


To resample a png in pyCairo you need this:
import cairo

img = cairo.ImageSurface.create_from_png("in.png")
width = img.get_width()
height = img.get_height()

imgpat = cairo.SurfacePattern(img)

scaler = cairo.Matrix()
#1 = 100%; 2 = 50%;0.5 = 200%
scaler.scale(2,2) #50% downscale in this case
imgpat.set_matrix(scaler)

#set resampling filter
imgpat.set_filter(cairo.FILTER_BEST)

canvas = cairo.ImageSurface(cairo.FORMAT_ARGB32,320,240)
ctx = cairo.Context(canvas)

ctx.set_source(imgpat)
ctx.paint()

canvas.write_to_png("out.png")



More information about the cairo mailing list