[cairo] image scaling in cairo, python

Wolfgang Lipp paragate at gmx.net
Sun May 25 05:00:09 PDT 2008


YAY, it WORKS! thanks so much, the first useful pointer in days. do you 
happen to know a site where people post useful recipes concerning cairo 
/ other graphics processing in pyhon (other than the obvious aspn 
cookbook)? any comprehensive guide to caito / pycairo?

you know i find it hard to figure out to do stuff like this in cairo. as 
soon as i know, it's not so bad, i'll simply wrap it as `def 
scale_image(...)` and presto. it's just those working code examples for 
various tasks that i need. i've already hatched a few methods to draw 
simple shapes, now i have raster scaling @ ffx3 quality. i am happy.

cheers & ~flow

Gerdus van Zyl wrote:
> 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")
>
> >From reading your posts to other lists I believe PIL is simpler. But
> it depends on your use case of course.
>
> ~Gerdus
>
>
> On Sat, May 24, 2008 at 12:04 PM, Wolfgang Lipp <paragate at gmx.net> wrote:
>   
>> [also posted to: comp.lang.python]
>>
>> hi all,
>>
>> i've heard cairo has become the image scling library for firefox3. is
>> that true? wonderful, i want to do that in python. there's a python
>> interface for cairo, right? i've used it before to do simple vector
>> stuff. seems to work. however, i haven't been able to find relevant
>> pointers via google. (rant: your mailman interface here is incredibly
>> unsuited to promote open discussions).
>>
>> so do you have any pointers on how to resize a raster image with python
>> using cairo? i've been jumping through hoops for a while now, and i
>> believe it should be easier.
>>
>> cheers and ~flow
>>
>> ps. related (long) posts:
>>
>> http://groups.google.com/group/pyglet-users/browse_frm/thread/44253ad01d809da5/cd051e6bced271e1#cd051e6bced271e1
>>
>> http://groups.google.com/group/comp.lang.python/browse_frm/thread/5df65d99cff0d7bb#
>>
>>
>> _______________________________________________
>> cairo mailing list
>> cairo at cairographics.org
>> http://lists.cairographics.org/mailman/listinfo/cairo
>>
>>     
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cairographics.org/archives/cairo/attachments/20080525/cdfdb531/attachment.htm 


More information about the cairo mailing list