crop image in skimage?

2019-03-29 08:01发布

问题:

I'm using skimage to crop a rectangle in a given image, now I have (x1,y1,x2,y2) as the rectangle coordinates, then I had loaded the image

 image = skimage.io.imread(filename)
 cropped = image(x1,y1,x2,y2)

However this is the wrong way to crop the image, how would I do it in the right way in skimage

回答1:

This seems a simple error on syntax.

Well, in Matlab you can use _'parentheses'_ to extract a pixel or an image region. But in Python, and numpy.ndarray you should use the brackets to slice a region of your image, besides in this code you is using the wrong way to cut a rectangle.

The right way to cut is using the : operator.

Thus,

from skimage import io
image = io.imread(filename)
cropped = image[x1:x2,y1:y2]


回答2:

you can go ahead with the Image module of the PIL library

from PIL import Image
im = Image.open("image.png")
im = im.crop((0, 50, 777, 686))
im.show()