In [1]:
from PIL import Image
import numpy as np

In [2]:
im = np.array(Image.open('data/src/lena_square.png'))

In [3]:
print(im.shape)


(512, 512, 3)

In [4]:
im_trim1 = im[128:384, 128:384]
print(im_trim1.shape)


(256, 256, 3)

In [5]:
Image.fromarray(im_trim1).save('data/dst/lena_numpy_trim.jpg')

In [6]:
def trim(array, x, y, width, height):
    return array[y:y + height, x:x+width]

In [7]:
im_trim2 = trim(im, 128, 192, 256, 128)
print(im_trim2.shape)


(128, 256, 3)

In [8]:
Image.fromarray(im_trim2).save('data/dst/lena_numpy_trim2.jpg')

In [9]:
im_trim3 = trim(im, 128, 192, 512, 128)
print(im_trim3.shape)


(128, 384, 3)

In [10]:
Image.fromarray(im_trim3).save('data/dst/lena_numpy_trim3.jpg')