3

I have a 2D array that I will save as a grayscale image using scipy.misc.toimage(). Before doing so, I want to skew the image by a given angle, interpolating like scipy.ndimage.interpolation.rotate():

enter image description here

The image above is just to illustrate the skewing process. I am aware that I have to enlarge my image in order to contain the skewed version. How can I achieve this? I would prefer to use scipy for this.

sodiumnitrate
  • 2,899
  • 6
  • 30
  • 49
  • This begs the question, why not just use `scipy.ndimage.interpolation.rotate()`? – DrBwts Oct 12 '15 at 15:52
  • 1
    @DrBwts Can `scipy.ndimage.interpolation.rotate()` do this? I don't want to rotate the whole image, I want to skew it such that only on of the axes is rotated. – sodiumnitrate Oct 12 '15 at 16:08
  • 3
    my apologies I misread your post, have a look at [this post](http://stackoverflow.com/questions/24191545/skewing-or-shearing-an-image-in-python) see if it helps. – DrBwts Oct 12 '15 at 16:17
  • 1
    PIL has shear/skew capabilities. Look up PIL Affine transformations – reptilicus Oct 12 '15 at 16:19
  • An example for PIL: http://stackoverflow.com/questions/14177744/how-does-perspective-transformation-work-in-pil – ivan_pozdeev Oct 12 '15 at 19:38

1 Answers1

3

This script can do that.

a=imread("sorNB.png")
h,l=a.shape
dl=50
b=numpy.zeros((h,l+dl),dtype=a.dtype)
for y in range(h):
    dec=(dl*(h-y))//h
    b[y,dec:dec+l]=a[y,:]

Since the inner assignment (b[y,dec:dec+l]=a[y,:]) is pure numpy, this is very fast.

EDIT

thanks to ivan_pozdeev. a way for interpolation :

from scipy.ndimage.interpolation import geometric_transform
a=imread("sorNB.png")
h,l=a.shape
def mapping(lc):
    l,c=lc
    dec=(dl*(l-h))/h
    return l,c+dec
figure(1)    
dl=50;c=geometric_transform(a,mapping,(h,l+dl),order=5,mode='nearest')
imshow (concatenate((a,zeros((225,50)),c),axis=-1),cmap=cm.gray)

sof

B. M.
  • 18,243
  • 2
  • 35
  • 54
  • 1
    From the looks of it, this doesn't do sub-pixel interpolation. Thus the "saw" pattern on the skewed lines. – ivan_pozdeev Oct 12 '15 at 19:16
  • Yes, as @ivan_pozdeev pointed out it does not interpolate. However, I might need this for certain other applications. Thank you for the input! – sodiumnitrate Oct 12 '15 at 20:26