def draw_image(self, x, y, im, origin, bbox): """ Draw the Image instance into the current axes; x is the distance in pixels from the left hand side of the canvas. y is the distance from the origin. That is, if origin is upper, y is the distance from top. If origin is lower, y is the distance from bottom """ #print 'draw_image' if bbox is not None: l, b, w, h = bbox.get_bounds() #rectangle = (int(l), self.height-int(b+h), # int(w), int(h)) # set clip rect? flipud = origin == 'lower' rows, cols, s = im.as_str(flipud) X = fromstring(s, UInt8) X.shape = rows, cols, 4 pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=1, bits_per_sample=8, width=cols, height=rows) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # pygtk was not compiled with Numeric Python support print >> sys.stderr, 'Error:', exc return
def draw_image(self, x, y, im, bbox): # bbox - not currently used if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) if numx.which[0] == "numarray": warnings.warn("draw_image() currently works for numpy, but not " "numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "draw_image()") return im.flipud_out() rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = numx.fromstring(buf, numx.UInt8) X.shape = rows, cols, 4 # function does not pass a 'gc' so use renderer.ctx ctx = self.ctx surface = cairo.ImageSurface.create_for_array(X) ctx.set_source_surface(surface, x, y) ctx.paint() im.flipud_out()
def draw_image(self, x, y, im, origin, bbox): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() try: import cairo.numpy except: verbose.report_error("cairo.numpy module required for draw_image()") return # bbox - not currently used flipud = origin=='lower' # not currently used ctx = cairo.Context() ctx.set_target_surface (self.surface) ctx.set_matrix (self.matrix) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = fromstring(buf, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 try: # works for numpy image, not a numarray image surface = cairo.numpy.surface_create_for_array (X) except TypeError, exc: verbose.report_error("%s: %s" % (_fn_name(), exc)) return
def draw_image(self, x, y, im, origin, bbox): if bbox != None: l, b, w, h = bbox.get_bounds() #rectangle = (int(l), self.height-int(b+h), # int(w), int(h)) # set clip rect? flipud = origin == 'lower' rows, cols, s = im.as_str(flipud) X = fromstring(s, UInt8) X.shape = rows, cols, 4 pb = gtk.gdk.Pixbuf( gtk.gdk.COLORSPACE_RGB, #has_alpha=1, bits_per_sample=8, has_alpha=True, bits_per_sample=8, width=cols, height=rows) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # pygtk was not compiled with Numeric Python support verbose.report_error('Error: %s' % exc) return
def draw_image(self, x, y, im, origin, bbox): if bbox != None: l, b, w, h = bbox.get_bounds() # rectangle = (int(l), self.height-int(b+h), # int(w), int(h)) # set clip rect? flipud = origin == "lower" rows, cols, image_str = im.as_str(flipud) image_array = fromstring(image_str, UInt8) image_array.shape = rows, cols, 4 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=cols, height=rows) array = pixbuf_get_pixels_array(pixbuf) array[:, :, :] = image_array gc = self.new_gc() if flipud: y = self.height - y - rows try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf(gc.gdkGC, pixbuf, 0, 0, int(x), int(y), cols, rows, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable( self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), cols, rows, gdk.RGB_DITHER_NONE, 0, 0 )
def _rgb(self, im): h, w, s = im.as_rgba_str() rgba = fromstring(s, UInt8) rgba.shape = (h, w, 4) rgb = rgba[:, :, :3] return h, w, rgb.tostring()
def _rgb(self, im): h,w,s = im.as_rgba_str() rgba = fromstring(s, UInt8) rgba.shape = (h, w, 4) rgb = rgba[:,:,:3] return h, w, rgb.tostring()
def draw_image(self, x, y, im, bbox): # bbox - not currently used if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) if numx.which[0] == "numarray": warnings.warn("draw_image() currently works for numpy, but not " "numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "draw_image()") return im.flipud_out() rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = numx.fromstring (buf, numx.UInt8) X.shape = rows, cols, 4 # function does not pass a 'gc' so use renderer.ctx ctx = self.ctx surface = cairo.ImageSurface.create_for_array (X) ctx.set_source_surface (surface, x, y) ctx.paint() im.flipud_out()
def load_hst_data(): """reconstruct the numerix arrays from the data files""" import matplotlib.numerix as n #hst = n.fromfile('hst.dat',typecode=n.UInt8, shape=(812,592,3))/255. str = open('data/hst.zdat').read() dstr = zlib.decompress(str) hst = n.fromstring(dstr, n.UInt8) hst.shape = (812, 592, 3) hst = hst/255. str = open('data/chandra.dat').read() chandra = n.fromstring(str, n.Int16) chandra.shape = (812,592) if sys.byteorder == 'little': chandra.byteswapped() # note that both HST and Chandra data are normalized to be between 0 and 1 return hst, chandra/16000.
def draw_image(self, x, y, im, origin, bbox): # bbox - not currently used if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) if numx.which[0] == "numarray": warnings.warn("draw_image() currently works for numpy, but not " "numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "draw_image()") return #flipud = origin=='lower' # gtk method (uses RGBA) #rows, cols, s = im.as_str(flipud) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = numx.fromstring (buf, numx.UInt8) X.shape = rows, cols, 4 # function does not pass a 'gc' so we create our own context ctx = cairo.Context (self.surface) surface = cairo.ImageSurface.create_for_array (X) ctx.set_source_surface (surface, x, y) ctx.paint()
def draw_image(self, x, y, im, origin, bbox): # bbox - not currently used if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) if numx.which[0] == "numarray": warnings.warn("draw_image() currently works for numpy, but not " "numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "draw_image()") return #flipud = origin=='lower' # gtk method (uses RGBA) #rows, cols, s = im.as_str(flipud) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = numx.fromstring(buf, numx.UInt8) X.shape = rows, cols, 4 # function does not pass a 'gc' so we create our own context ctx = cairo.Context(self.surface) surface = cairo.ImageSurface.create_for_array(X) ctx.set_source_surface(surface, x, y) ctx.paint()
def load_hst_data(): """reconstruct the numerix arrays from the data files""" import matplotlib.numerix as n #hst = n.fromfile('hst.dat',typecode=n.UInt8, shape=(812,592,3))/255. str = open('data/hst.zdat').read() dstr = zlib.decompress(str) hst = n.fromstring(dstr, n.UInt8) hst.shape = (812, 592, 3) hst = hst / 255. str = open('data/chandra.dat').read() chandra = n.fromstring(str, n.Int16) chandra.shape = (812, 592) if sys.byteorder == 'little': chandra.byteswapped() # note that both HST and Chandra data are normalized to be between 0 and 1 return hst, chandra / 16000.
def get_two_stock_data(): """ load stock time and price data for two stocks The return values (d1,p1,d2,p2) are the trade time (in days) and prices for stocks 1 and 2 (intc and aapl) """ ticker1, ticker2 = 'INTC', 'AAPL' M1 = fromstring( file('data/%s.dat' % ticker1, 'rb').read(), 'd') M1 = M1.resize( (M1.shape[0]/2,2) ) M2 = fromstring( file('data/%s.dat' % ticker2, 'rb').read(), 'd') M2 = M2.resize( (M2.shape[0]/2,2) ) d1, p1 = M1[:,0], M1[:,1] d2, p2 = M2[:,0], M2[:,1] return (d1,p1,d2,p2)
def _gray(self, im, flipud, rc=0.3, gc=0.59, bc=0.11): rgbat = im.as_str(flipud) rgba = fromstring(rgbat[2], UInt8) rgba.shape = (rgbat[0], rgbat[1], 4) r = rgba[:, :, 0].astype(Float32) g = rgba[:, :, 1].astype(Float32) b = rgba[:, :, 2].astype(Float32) gray = (r * rc + g * gc + b * bc).astype(UInt8) return rgbat[0], rgbat[1], gray.tostring()
def _gray(self, im, flipud, rc=0.3, gc=0.59, bc=0.11): rgbat = im.as_str(flipud) rgba = fromstring(rgbat[2], UInt8) rgba.shape = (rgbat[0], rgbat[1], 4) r = rgba[:,:,0].astype(Float32) g = rgba[:,:,1].astype(Float32) b = rgba[:,:,2].astype(Float32) gray = (r*rc + g*gc + b*bc).astype(UInt8) return rgbat[0], rgbat[1], gray.tostring()
def _gray(self, im, rc=0.3, gc=0.59, bc=0.11): rgbat = im.as_rgba_str() rgba = fromstring(rgbat[2], UInt8) rgba.shape = (rgbat[0], rgbat[1], 4) rgba_f = rgba.astype(Float32) r = rgba_f[:, :, 0] g = rgba_f[:, :, 1] b = rgba_f[:, :, 2] gray = (r * rc + g * gc + b * bc).astype(UInt8) return rgbat[0], rgbat[1], gray.tostring()
def _gray(self, im, rc=0.3, gc=0.59, bc=0.11): rgbat = im.as_rgba_str() rgba = fromstring(rgbat[2], UInt8) rgba.shape = (rgbat[0], rgbat[1], 4) rgba_f = rgba.astype(Float32) r = rgba_f[:,:,0] g = rgba_f[:,:,1] b = rgba_f[:,:,2] gray = (r*rc + g*gc + b*bc).astype(UInt8) return rgbat[0], rgbat[1], gray.tostring()
def draw_image(self, x, y, im, origin, bbox): """ Draw the Image instance into the current axes; x is the distance in pixels from the left hand side of the canvas. y is the distance from the origin. That is, if origin is upper, y is the distance from top. If origin is lower, y is the distance from bottom origin is 'upper' or 'lower' bbox is a matplotlib.transforms.BBox instance for clipping, or None """ if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() try: import cairo.numpy except: verbose.report_error( "cairo.numpy module required for draw_image()") return # bbox - not used flipud = origin == 'lower' ctx = cairo.Context() ctx.set_target_surface(self.surface) ctx.set_matrix(self.matrix) rows, cols, buffer = im.buffer_argb32( ) # ARGB32, but colors still wrong X = fromstring(buffer, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 surface = cairo.numpy.surface_create_for_array(X) # Alternative #surface = cairo.surface_create_for_image(buffer, cairo.FORMAT_ARGB32, cols, rows) #, stride) # error: TypeError: Cannot use string as modifiable buffer ctx.translate(x, y) ctx.show_surface(surface, cols, rows)
def _draw_mathtext(self, gc, x, y, s, prop, angle): if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) # mathtext using the gtk/gdk method if numx.which[0] == "numarray": warnings.warn("_draw_mathtext() currently works for numpy, but " "not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "_draw_mathtext()") return size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = numx.zeros((N,len(fonts)), typecode=numx.UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:,i] = numx.fromstring(s, numx.UInt8) # get the max alpha at each pixel Xs = numx.mlab.max (Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pa = numx.zeros(shape=(imh,imw,4), typecode=numx.UInt8) rgb = gc.get_rgb() pa[:,:,0] = int(rgb[0]*255) pa[:,:,1] = int(rgb[1]*255) pa[:,:,2] = int(rgb[2]*255) pa[:,:,3] = Xs # works for numpy pa, not a numarray pa surface = cairo.ImageSurface.create_for_array (pa) gc.ctx.set_source_surface (surface, x, y) gc.ctx.paint()
def _draw_mathtext(self, gc, x, y, s, prop, angle): if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name()) # mathtext using the gtk/gdk method if numx.which[0] == "numarray": warnings.warn("_draw_mathtext() currently works for numpy, but " "not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo with Numeric support is required for " "_draw_mathtext()") return size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = numx.zeros((N, len(fonts)), typecode=numx.UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:, i] = numx.fromstring(s, numx.UInt8) # get the max alpha at each pixel Xs = numx.mlab.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pa = numx.zeros(shape=(imh, imw, 4), typecode=numx.UInt8) rgb = gc.get_rgb() pa[:, :, 0] = int(rgb[0] * 255) pa[:, :, 1] = int(rgb[1] * 255) pa[:, :, 2] = int(rgb[2] * 255) pa[:, :, 3] = Xs # works for numpy pa, not a numarray pa surface = cairo.ImageSurface.create_for_array(pa) gc.ctx.set_source_surface(surface, x, y) gc.ctx.paint()
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x -= width y -= height imw, imh, image_str = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = zeros((N, len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, image_str = font.image_as_str() Xall[:, i] = fromstring(image_str, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=imw, height=imh) array = pixbuf_get_pixels_array(pixbuf) rgb = gc.get_rgb() array[:, :, 0] = int(rgb[0] * 255) array[:, :, 1] = int(rgb[1] * 255) array[:, :, 2] = int(rgb[2] * 255) array[:, :, 3] = Xs try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf(gc.gdkGC, pixbuf, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0)
def _draw_mathtext(self, gc, x, y, s, prop, angle): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() # mathtext using the gtk/gdk method if numerix.which[0] == "numarray": warnings.warn("_draw_mathtext() currently works for numpy, but not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo.numpy module required for _draw_mathtext()") return size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = zeros((N,len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:,i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pa = zeros(shape=(imh,imw,4), typecode=UInt8) rgb = gc.get_rgb() pa[:,:,0] = int(rgb[0]*255) pa[:,:,1] = int(rgb[1]*255) pa[:,:,2] = int(rgb[2]*255) pa[:,:,3] = Xs # works for numpy pa, not a numarray pa surface = cairo.numpy.surface_create_for_array(pa) gc.ctx.translate (x,y) gc.ctx.show_surface (surface, imw, imh)
def draw_image(self, x, y, im, origin, bbox): """ Draw the Image instance into the current axes; x is the distance in pixels from the left hand side of the canvas. y is the distance from the origin. That is, if origin is upper, y is the distance from top. If origin is lower, y is the distance from bottom origin is 'upper' or 'lower' bbox is a matplotlib.transforms.BBox instance for clipping, or None """ if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() try: import cairo.numpy except: verbose.report_error("cairo.numpy module required for draw_image()") return # bbox - not used flipud = origin=='lower' ctx = cairo.Context() ctx.set_target_surface (self.surface) ctx.set_matrix (self.matrix) rows, cols, buffer = im.buffer_argb32() # ARGB32, but colors still wrong X = fromstring(buffer, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 surface = cairo.numpy.surface_create_for_array (X) # Alternative #surface = cairo.surface_create_for_image(buffer, cairo.FORMAT_ARGB32, cols, rows) #, stride) # error: TypeError: Cannot use string as modifiable buffer ctx.translate (x,y) ctx.show_surface (surface, cols, rows)
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, image_str = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = zeros((N,len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, image_str = font.image_as_str() Xall[:,i] = fromstring(image_str, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=imw, height=imh) array = pixbuf_get_pixels_array(pixbuf) rgb = gc.get_rgb() array[:,:,0]=int(rgb[0]*255) array[:,:,1]=int(rgb[1]*255) array[:,:,2]=int(rgb[2]*255) array[:,:,3]=Xs try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf (gc.gdkGC, pixbuf, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), imw, imh, gdk.RGB_DITHER_NONE, 0, 0)
def draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x = int(x) y = int(y) rgb = gc.get_rgb() #rgba = (rgb[0], rgb[1], rgb[2], gc.get_alpha()) imw, imh, s = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = zeros((N, len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:, i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=1, bits_per_sample=8, width=imw, height=imh) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # pygtk was not compiled with Numeric Python support print >> sys.stderr, 'Error:', exc return
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font(s, self.dpi.get(), size) if angle == 90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw * imh # a numpixels by num fonts array Xall = zeros((N, len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:, i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.max(Xall, 1) # convert it to it's proper shape Xs.shape = imh, imw pb = gtk.gdk.Pixbuf( gtk.gdk.COLORSPACE_RGB, #has_alpha=1, bits_per_sample=8, width=imw, height=imh) has_alpha=True, bits_per_sample=8, width=imw, height=imh) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # 'pygtk was not compiled with Numeric Python support' verbose.report_error('mathtext not supported: %s' % exc) return
def draw_image(self, x, y, im, origin, bbox): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() if numerix.which[0] == "numarray": warnings.warn( "draw_image() currently works for numpy, but not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo.numpy module required for draw_image()") return # bbox - not currently used flipud = origin == 'lower' # not currently used ctx = cairo.Context() ctx.set_target_surface(self.surface) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = fromstring(buf, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 # works for numpy X, not numarray X surface = cairo.numpy.surface_create_for_array(X) # Alternative #surface = cairo.surface_create_for_image(buf, cairo.FORMAT_ARGB32, cols, rows) #, stride) # error: TypeError: Cannot use string as modifiable buffer ctx.translate(x, y) ctx.show_surface(surface, cols, rows)
def draw_image(self, x, y, im, origin, bbox): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() if numerix.which[0] == "numarray": warnings.warn("draw_image() currently works for numpy, but not numarray") return if not HAVE_CAIRO_NUMPY: warnings.warn("cairo.numpy module required for draw_image()") return # bbox - not currently used flipud = origin=='lower' # not currently used ctx = cairo.Context() ctx.set_target_surface (self.surface) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = fromstring(buf, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 # works for numpy X, not numarray X surface = cairo.numpy.surface_create_for_array (X) # Alternative #surface = cairo.surface_create_for_image(buf, cairo.FORMAT_ARGB32, cols, rows) #, stride) # error: TypeError: Cannot use string as modifiable buffer ctx.translate (x,y) ctx.show_surface (surface, cols, rows)
def draw_image(self, x, y, im, origin, bbox): if bbox != None: l, b, w, h = bbox.get_bounds() # rectangle = (int(l), self.height-int(b+h), # int(w), int(h)) # set clip rect? flipud = origin == "lower" rows, cols, s = im.as_str(flipud) X = fromstring(s, UInt8) X.shape = rows, cols, 4 pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=1, bits_per_sample=8, width=cols, height=rows) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # pygtk was not compiled with Numeric Python support verbose.report_error("Error: %s" % exc) return
def draw_image(self, x, y, im, origin, bbox): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() # works for numpy image, not a numarray image try: import cairo.numpy except: verbose.report_error( "cairo.numpy module required for draw_image()") return # bbox - not currently used flipud = origin == 'lower' # not currently used ctx = cairo.Context() ctx.set_target_surface(self.surface) ctx.set_matrix(self.matrix) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = fromstring(buf, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 surface = cairo.numpy.surface_create_for_array(X) # Alternative #surface = cairo.surface_create_for_image(buf, cairo.FORMAT_ARGB32, cols, rows) #, stride) # error: TypeError: Cannot use string as modifiable buffer ctx.translate(x, y) ctx.show_surface(surface, cols, rows)
def draw_image(self, x, y, im, bbox): if bbox != None: l, b, w, h = bbox.get_bounds() #rectangle = (int(l), self.height-int(b+h), # int(w), int(h)) # set clip rect? im.flipud_out() rows, cols, image_str = im.as_rgba_str() image_array = fromstring(image_str, UInt8) image_array.shape = rows, cols, 4 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True, bits_per_sample=8, width=cols, height=rows) array = pixbuf_get_pixels_array(pixbuf) array[:, :, :] = image_array gc = self.new_gc() y = self.height - y - rows try: # new in 2.2 # can use None instead of gc.gdkGC, if don't need clipping self.gdkDrawable.draw_pixbuf(gc.gdkGC, pixbuf, 0, 0, int(x), int(y), cols, rows, gdk.RGB_DITHER_NONE, 0, 0) except AttributeError: # deprecated in 2.2 pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0, int(x), int(y), cols, rows, gdk.RGB_DITHER_NONE, 0, 0) # unflip im.flipud_out()
def _draw_mathtext(self, gc, x, y, s, prop, angle): size = prop.get_size_in_points() width, height, fonts = math_parse_s_ft2font( s, self.dpi.get(), size) if angle==90: width, height = height, width x -= width y -= height imw, imh, s = fonts[0].image_as_str() N = imw*imh # a numpixels by num fonts array Xall = zeros((N,len(fonts)), typecode=UInt8) for i, font in enumerate(fonts): if angle == 90: font.horiz_image_to_vert_image() # <-- Rotate imw, imh, s = font.image_as_str() Xall[:,i] = fromstring(s, UInt8) # get the max alpha at each pixel Xs = numerix.mlab.max(Xall,1) # convert it to it's proper shape Xs.shape = imh, imw pb=gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, #has_alpha=1, bits_per_sample=8, width=imw, height=imh) has_alpha=True, bits_per_sample=8, width=imw, height=imh) try: pa = pb.get_pixels_array() except AttributeError: pa = pb.pixel_array except RuntimeError, exc: # 'pygtk was not compiled with Numeric Python support' verbose.report_error('mathtext not supported: %s' % exc) return
def draw_image(self, x, y, im, origin, bbox): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() # works for numpy image, not a numarray image try: import cairo.numpy except: verbose.report_error("cairo.numpy module required for draw_image()") return # bbox - not currently used flipud = origin=='lower' # not currently used ctx = cairo.Context() ctx.set_target_surface (self.surface) ctx.set_matrix (self.matrix) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = fromstring(buf, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 surface = cairo.numpy.surface_create_for_array (X) # Alternative #surface = cairo.surface_create_for_image(buf, cairo.FORMAT_ARGB32, cols, rows) #, stride) # error: TypeError: Cannot use string as modifiable buffer ctx.translate (x,y) ctx.show_surface (surface, cols, rows)
def draw_image(self, x, y, im, origin, bbox): if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name() try: import cairo.numpy except: verbose.report_error( "cairo.numpy module required for draw_image()") return # bbox - not currently used flipud = origin == 'lower' # not currently used ctx = cairo.Context() ctx.set_target_surface(self.surface) ctx.set_matrix(self.matrix) rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong X = fromstring(buf, UInt8) X.shape = rows, cols, 4 #print dir(im) #print 'im.get_size()', im.get_size() #print 'r,c', rows, cols # GTK method #rows, cols, s = im.as_str(flipud) # RGBA #print 'r,c', rows, cols #X = fromstring(s, UInt8) #X.shape = rows, cols, 4 # ARGB32 try: # works for numpy image, not a numarray image surface = cairo.numpy.surface_create_for_array(X) except TypeError, exc: verbose.report_error("%s: %s" % (_fn_name(), exc)) return
def _rgb(self, im, flipud): rgbat = im.as_str(flipud) rgba = fromstring(rgbat[2], UInt8) rgba.shape = (rgbat[0], rgbat[1], 4) rgb = rgba[:, :, :3] return rgbat[0], rgbat[1], rgb.tostring()
def _rgb(self, im, flipud): rgbat = im.as_str(flipud) rgba = fromstring(rgbat[2], UInt8) rgba.shape = (rgbat[0], rgbat[1], 4) rgb = rgba[:,:,:3] return rgbat[0], rgbat[1], rgb.tostring()