Frage

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()
War es hilfreich?

Lösung

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()

Andere Tipps

What about recreating this particular image using PDF commands? It's vector format and you may design similar image in svg and import it.Image scaling is a fatal process of loosing actual color data, cause approximation takes place. Use advanced scaling algorithm, Lanczos filtering for example.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top