views:

41

answers:

1

Hi

My question relates to Python GTK

I have an image -a JPG - which I draw onto a drawing area. I want to reveal a portion of the image -say a 10pix by 10 px square -only where the mouse pointer is currently at. Everything 10 x 10 px square away from the mouse should hidden i.e. black.

I'm am new to PyGtk please can anyone help?

Thanks

A: 
#!/usr/bin/python                                      

import os
import sys
import gtk

MASK_COLOR = 0x000000

def composite(source, start_x=345, start_y=345):
  width = 50                                    
  height  = 50                                  
  alpha = 255                                   
  dest = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8 ,800,800)
  dest.fill(MASK_COLOR)                                           
  source.composite(dest,                                          
                   start_x,                                       
                   start_y,
                   width,
                   height,
                   0,
                   0,
                   1,
                   1,
                   gtk.gdk.INTERP_NEAREST,
                   alpha)

  return dest


def it_moved(widget, event, window, masked, original):
  r = window.get_display().get_window_at_pointer()
  masked.set_from_pixbuf(composite(original.get_pixbuf(), r[1], r[2]))
  return True


if __name__ == '__main__':
  window = gtk.Window()
  eb = gtk.EventBox()
  original = gtk.Image()
  original.set_from_file(sys.argv[1])

  masked = gtk.Image()
  masked.set_from_pixbuf(composite(original.get_pixbuf()))

  eb.add(masked)
  eb.set_property('events', gtk.gdk.POINTER_MOTION_MASK)
  eb.connect('motion_notify_event', it_moved, window, masked, original)
  window.add(eb)
  window.set_size_request(800,800)
  window.show_all()
  gtk.main()

This should do something like you describe. I chose to show a 50x50 area since yours was kinda small to see under the pointer. I didn't hide that either.

GregMeno