Mahotas – Cropping Image

In this article we will see how we can crop the image in mahotas. Cropping is easily done simply by slicing the correct part out of the array, here array is the image object which is numpy.ndarray.
In this tutorial we will use “luispedro” image, below is the command to load it.
mahotas.demos.load('luispedro')
Below is the luispedro image
In order to do this we will take the image object which is numpy.ndarray and filter it with the help of indexing, below is the command to do this
image = image[i1:i2, j1:j2]
Example 1:
Python3
# importing required librariesimport numpy as npimport mahotasimport mahotas.demosfrom mahotas.thresholding import soft_thresholdfrom matplotlib import pyplot as pltfrom os import path# loading image as greyf = mahotas.demos.load('luispedro', as_grey = True)# making plt greyplt.gray()# showing imageprint("Image")plt.imshow(f)plt.show()# cropping imagef = f[50:200, 20: 250]# Show the imageprint("Cropped Image")plt.imshow(f)plt.show() |
Output :
Example 2:
Python3
# importing required librariesimport mahotasimport numpy as npfrom pylab import imshow, showimport os# loading imageimg = mahotas.imread('dog_image.png')# showing imageprint("Image")imshow(img)show() # cropping imageimg = img[:, 200:700]# showing the imageprint("Cropped Image")imshow(img)show() |
Output :




