7

I need to shear and skew some images using Python. I've come across this skimage module but I don't really understand how I'm supposed to use this.

I've tried a few things which ended up in errors, because I realized later that I wasn't passing in my image to the function. I then noticed that the function doesn't take my image as an input parameter in the first place. So, how should the transformation be applied? Or, is this not the correct function to be looking at in order to skew or shear an image?

Community
  • 1
  • 1
user961627
  • 12,379
  • 42
  • 136
  • 210
  • 1
    How about [PIL](https://pillow.readthedocs.org/en/1.7.8/pythondoc-PIL.ImageTransform.html) and [affine transforms](http://en.wikipedia.org/wiki/Affine_transformation)? – Paulo Scardine Jun 12 '14 at 18:50
  • 3
    Have a look at http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html – Mark Ransom Jun 12 '14 at 18:51

1 Answers1

23

If you want to use the skimage module the order of operations are:

  • Load image or define data to work with
  • Create the transformation you want
  • Apply the transformatioin

A work flow might look like the following:

from skimage import io
from skimage import transform as tf

# Load the image as a matrix
image = io.imread("/path/to/your/image.jpg")

# Create Afine transform
afine_tf = tf.AffineTransform(shear=0.2)

# Apply transform to image data
modified = tf.warp(image, inverse_map=afine_tf)

# Display the result
io.imshow(modified)
io.show()

The AffineTransform class from the skimage module accepts a transformation matrix as its first parameter (which the class constructs if you instead use the other parameters).

Eric Platon
  • 9,819
  • 6
  • 41
  • 48
MrAlias
  • 1,316
  • 15
  • 26
  • The example should actually use `from skimage import io; image = io.imread('path/to/image.jpg')`, or something like `image = data.lena()`. `data.imread` only exists because it's imported from the `io` module in order to read images. Nevertheless, great example! – Tony S Yu Jun 13 '14 at 04:58
  • Thanks, exactly what I needed. Just one thing - in my code I used `img = color.rgb2gray(io.imread("path_to_my_image"))` and `ImageViewer` to view the image. Otherwise using `matplotlib.pyplot` generated an image that showed everything in shades of the primary colors. – user961627 Jun 13 '14 at 08:07
  • The "primary colors" is what happens when you do ``imshow`` on a gray-level image. Usually you want to do ``imshow(image, cmap='gray')``. – Stefan van der Walt Jun 13 '14 at 18:14
  • How can I prevent the skewed image from bleeding out? – sodiumnitrate Oct 12 '15 at 17:31
  • You can also adjust the `rotation` argument in radians to rotate on the x,y axes. [you can see the here in the documentation](http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.AffineTransform) – M090009 Apr 20 '18 at 11:04