I change the api to support an arbitrary affine transform. The current api is
def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None): dx, dy is the width and height of the image, i.e., The 4 edge points of the image will be (x, y), (x+dx, y), (x+dx, y+dy), (x, y+dy) after transformed by *transform*. And if transform involves rotation or skew, the image should also be rotated and skewed. Attached is a small example to utilized this functionality. Note that it only works with the ps-backend. I personally don't have much use case for rotated or skewed images. So, it would be great if others can suggest some user-space api. What I have in my mind is to extend the "extent" keyword of the imshow and make it optionally take a tuple of 6 numbers, which are (x1, x_lrc, x2, y1, y_lrc, y2). x1, x2, y1, y2 are same as the original "extent", and the (x_lrc, y_lrc) represent the coordinate of the lower-right corner of the image. So, the imshow in the attached example will become somthing like im = plt.imshow(Z, interpolation='nearest', cmap=cm.jet, origin='lower', extent=[-2, 3, 4, -3, -2, 2]) And, the transformation will be calculated internally by the Image class. Any thought? -JJ On Wed, Dec 16, 2009 at 3:41 PM, Andrew Straw <straw...@astraw.com> wrote: > Jae-Joon Lee wrote: >> >> On Wed, Dec 16, 2009 at 12:59 PM, Jouni K. Seppänen <j...@iki.fi> wrote: >> >>> >>> While the backend API is being changed, would it be similarly easy to >>> support arbitrary affine transformations? It would make the API more >>> symmetric, since many other draw_* methods take an affine >>> transformation, and I guess at least the PS and PDF backends could >>> easily support this. >>> >>> >> >> Yes, supporting arbitrary affine transform would not be difficult. On >> the other hand, I'm not sure if that feature is practically useful, >> assuming that other rasterizing backend won't implement it. I'll think >> about it. > > I second the notion that affine transformations on images would be useful. > There are a lot of things one can do with this, such as make > pseudo-perspective projections of texture-mapped rectangles. This is exactly > one of the things that I routinely do in Inkscape. > > I'm reasonably sure Cairo supports arbitrary affine transformations when > drawing image data -- I don't know about Agg. > > -Andrew >
#!/usr/bin/env python import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt import matplotlib.transforms as mtransforms delta = 0.25 x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = Z2-Z1 # difference of Gaussians im = plt.imshow(Z, interpolation='nearest', cmap=cm.jet, origin='lower', extent=[-2, 4, -3, 2]) #plt.savefig("test.eps") def get_rotate_and_skew_transform(im, x3, y3): x1, x2, y1, y2 = im.get_extent() tr1 = mtransforms.Affine2D() tr1.translate(-x1, -y1) x2a, y2a = tr1.transform_point((x2, y2)) x3a, y3a = tr1.transform_point((x3, y3)) inv_mat = 1./(x2a*y3a-y2a*x3a) * np.mat([[y3a, -y2a],[-x3a, x2a]]) a, b = (inv_mat * np.mat([[x2a], [x2a]])).flat c, d = (inv_mat * np.mat([[y2a], [0]])).flat tr2 = mtransforms.Affine2D.from_values(a, c, b, d, 0, 0) tr = (tr1 + tr2 + mtransforms.Affine2D().translate(x1, y1)).inverted().get_affine() return tr x1, x2, y1, y2 = im.get_extent() x3, y3 = 3, -2 tr = get_rotate_and_skew_transform(im, x3, y3) # anchor points in circle plt.plot([x1, x2, x3], [y1, y2, y3], "o") # original extent in black plt.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "k-") trans2 = tr + plt.gca().transData.get_affine() im.set_transform(trans2) # transformed extent in red plt.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "r-", transform=trans2) plt.savefig("a.eps")
------------------------------------------------------------------------------ This SF.Net email is sponsored by the Verizon Developer Community Take advantage of Verizon's best-in-class app development support A streamlined, 14 day to market process makes app distribution fast and easy Join now and get one step closer to millions of Verizon customers http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________ Matplotlib-devel mailing list Matplotlib-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-devel