2

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()
user2435611
  • 1,093
  • 1
  • 12
  • 18
  • When you scale a (bitmapped) image down, unless you're simultaneously increasing its dpi, you're _always_ losing quality. There's no way around that. A 200x200 image only has 4/9ths the information of a 300x300 image, and you can't throw away 55% of your information without losing quality. Averaging, blurring, dithering, and other techniques are attempts to make the loss less visible; they all have obvious side effects, but not using them has even more obvious side effects. – abarnert Jul 03 '13 at 17:51
  • I believe the question is exactly about increasing dpi, as output is PDF, essentially a vector format. – Dima Tisnek Jul 03 '13 at 19:15

2 Answers2

0

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()
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120
  • np. to add a caveat, say your user dumped a 80Kx80K 16-bits-per-channel rgba image, that will result in a huge pdf, in that case you may want to prescale raster image :) – Dima Tisnek Jul 04 '13 at 08:36
0

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.

Mico
  • 121
  • 1