gtk.gdk.Pixbuf.scale() unexpected behavior when offset != 0

Joel Hedlund joel.hedlund at gmail.com
Tue Jun 17 05:42:06 EDT 2008


Hi!

I'm developing a pygtk application where I need to show images zoomed in 
so that the user can see individual pixels. gtk.gdk.Pixbuf.scale() 
seemed ideal for this, but if I set offset_x and offset_y to anything 
other than 0, the resulting image is heavily distorted and the offset is 
wrong. I've searched the internet for any snippet of code that uses this 
function with nonzero offset, but even after several hours of searching 
I've still come up blank. I think this may be a bug, but since it seems 
so fundamental I think it's way more likely that I've misunderstood 
something, so I thought I'd pass this by c.l.p first. Any help is 
greatly appreciated.

I wrote a test program to show off this behavior. Please find attached 
an image of David Hasselhoff with some puppies to help facilitate this 
demonstration. (feel free to use any image, but who doesn't like 
Hasselhoff and puppies?)

show_hasselhoff.py
#-------------------------------------------------------
import gtk

original = gtk.gdk.pixbuf_new_from_file('hasselhoff.jpeg')
w = original.get_width()
h = original.get_height()
interp = gtk.gdk.INTERP_NEAREST

nice = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, w, h)
ugly = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, w, h)
original.scale(nice, 0, 0, w, h, 0, 0, 2, 2, interp)
original.scale(ugly, 0, 0, w, h, w/2, h/2, 2, 2, interp)

outtake = original.subpixbuf(w/4, h/4, w/2, w/2)
expected = outtake.scale_simple(w, h, interp)

w = gtk.Window()
hbox = gtk.HBox()
hbox.add(gtk.image_new_from_pixbuf(original))
hbox.add(gtk.image_new_from_pixbuf(nice))
hbox.add(gtk.image_new_from_pixbuf(ugly))
hbox.add(gtk.image_new_from_pixbuf(expected))
w.add(hbox)
w.show_all()
w.connect('destroy', gtk.main_quit)
gtk.main()
#-------------------------------------------------------

When you run this, you should see 4 images in a window. From left to 
right: original, nice, ugly and expected. nice, ugly and expected are 
scaled/cropped copies of original, but ugly and expected are offset to 
show less mullet and more face. expected is what I expected ugly to turn 
out like judging from the pygtk docs.

Things to note about ugly:
* The topleft pixel of original has been stretched to the area of 
offset_x * offset_y.
* The first offset_x - 1 top pixels of original have been scaled by a 
factor 2 horizontally and then stretched vertically to the height of 
offset_y.
* Vice versa for the first offset_y - 1 leftmost pixels of original.
* The remaining area of ugly is a scaled version of 
original(1,1,width/2-1,height/2-1).

Things to note about the methods:
* This behavior is constant for all interpolation methods.
* This behavior is identical in gtk.gdk.Pixbuf.compose().

This can't possibly be how this is supposed to work! Have I 
misunderstood something, or is this a bug?

Cheers!
/Joel Hedlund
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hasselhoff.jpeg
Type: image/jpeg
Size: 2937 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080617/1a827d4a/attachment.jpeg>


More information about the Python-list mailing list