That's cool. Is this in the faq? We should probably add this whole thread into it if it's not. I'm writing a pyGTK chapter for a new book as we speak, so I'll make sure to add it there at least. :>

-dave

Guillaume Proux wrote:

To do a ImageGrab in Windows only, you can also use the following:

Install PIL (from effbot)

screengrab = ImageGrab.grab()

done !

Guillaume

Dave Aitel wrote:

This is some more, even more horrible, code for handling windows screengrabs. (In CANVAS we grab screenshots using a little proglet we inject into a remote process, but it does sound similar to what you're doing here.)

You can view resultant images like so, under Linux:
self.log("Result: display -depth 8 -size %sx%s rgb:%s"%(hor,vert,filename))


I included some code we use to display it in a little window as well - this is known to work under Windows or Linux.

-dave

#Immunity grants this snippet under Python Licence

def update_Images(self, area, event):
self.pangolayout = self.area.create_pango_layout("")
self.style = self.area.get_style()
self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
self.gc = self.style.black_gc
buff= self.read_Image(self.filename)
if self.area.window == None:
return
self.area.window.draw_rgb_image(self.gc, 0, 0, self.Iwidth, self.Iheight,
gtk.gdk.RGB_DITHER_NONE, buff, self.Iwidth*3)
self.area.window.draw_layout(self.gc, 0, 0, self.pangolayout)
self.area.show()
def show_Images(self, obj, event):
if event.type==gtk.gdk._2BUTTON_PRESS:
model,iter=self.iTree.get_selection().get_selected()
if not iter:
return
description = model.get_value(iter,2)
self.filename = model.get_value(iter,0)
wTemp = gtk.glade.XML("canvasgui2.glade","sImage")


           window=wTemp.get_widget("sImage")
           window.set_title(description)

           (width,height) = description.split(" ", 1)[0].split("x")
                     self.Iwidth=int(width)
           self.Iheight=int(height)
           #print "Image width=%d height=%d"%(self.Iwidth,self.Iheight)
           self.area = wTemp.get_widget("drawingarea2")
           self.area.set_size_request(self.Iwidth, self.Iheight)
           # This is the trick to make a DrawImage works
           self.area.connect("expose_event", self.update_Images)

def normalizergb(x,y,data,alpha=3):
   """
   Windows screengrabs are in BGRA order, and need to be flipped
   Set alpha=3 for no alpha, 4 for alpha
   """
   print "Normalizing RGB for win32"

   ret=[]
   length=len(data)
   if length%alpha!=0:
       print "Very strange - is not mod %s clean!"%alpha

   for i in range(0,y):
       #for each scanline

       for j in range (0,x):
           #for each pixel
                     #print "Doing: %s:%s"%(i,j)
           b=data[x*i*alpha +alpha*j+0]
           g=data[x*i*alpha +alpha*j+1]
           r=data[x*i*alpha +alpha*j+2]
             ret+=[r,g,b]
   data="".join(ret)
   print "Returning %s"%len(data)
   return data

def verticleflip(x,y,data,alpha=3):
   """
   Windows screenshots need to be flipped verticly
   alpha=4 for alpha byte as well
   """
   print "Flipping verticly"
   i=0
   ret=[]
   length=len(data)
   if length%alpha!=0:
       print "Very strange - is not mod %s clean!"%alpha

   while i<length:
       line=data[i:i+x*alpha]
       ret=[line]+ret
       i+=x*alpha
   return "".join(ret)




_______________________________________________ pygtk mailing list [email protected] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to