pythoncairopixbuf

How to scale down pixbuf image without losing quality?


I have a pixbuf image and I want to save it to pdf using cairo. Because the pixbuf is too large, I want to scale it down. I use scale_simple method. But, the scaled result became blur. Below is the screenshot I take. The real image is on the right and on the left is image from pdf blur

Do you know how to scale down pixbuf without losing its quality? Below is just my sample code.

from gi.repository import GdkPixbuf, Gdk
import cairo

class gui():
    def __init__(self):
        pix = GdkPixbuf.Pixbuf.new_from_file('tux2.png')
        pix = pix.scale_simple(pix.get_width() / 3, pix.get_height() / 3, GdkPixbuf.InterpType.HYPER)
        ps = cairo.PDFSurface('pix.pdf', 500, 500)
        cr = cairo.Context(ps)
        Gdk.cairo_set_source_pixbuf(cr, pix, 0, 0)
        cr.paint()

if __name__ == '__main__':
    gui()

Solution

  • You should not scale the pixbuf at all.

    Instead you should scale the object (pixbuf as is).

    It will look something like this:

    cr.save()
    cr.scale(scale_xy, scale_xy)
    cr.xxx_place_image(...)
    cr.restore()