Antonio López Ruiz
Antonio López Ruiz

Reputation: 1466

Cropping image by the center

I have a PNG image of size 218, 178. I am using matplotlib's function imread to convert it into a ndarray. I would like to crop it to get the middle 64X64 part of the image.

I tried cropping with np.reshape, but it makes no sense. I also tried slicing as a normal array, but I can't get it right since the actual array has a shape of (218,178,3). I want it (64,64,3) taking from 77 to 141 and 57 to 121 for the first two dimensions.

Upvotes: 4

Views: 24874

Answers (2)

benson
benson

Reputation: 443

You want to slice the first two axes of the numpy array, which correspond to height and width, respectively (the third is colour channel).

import matplotlib.pyplot as pl

# load image
img = pl.imread('my_image.png')

# confirm image shape
print(img.shape)

(218, 178, 3)

These three numbers correspond to the size of each axis, which for an image are usually interpreted as: (height, width, depth/colors).

# crop image
img_cropped = img[77:141, 57:121, :]

# confirm cropped image shape
print(img_cropped.shape)

(64, 64, 3)

Also note that when cropping, you could have also omitted the last colon: img[77:141, 57:121]

Upvotes: 11

Fabiano Battaglin
Fabiano Battaglin

Reputation: 90

Cropping can be easily done simply by slicing the correct part out of the array. E.g. image[100:200, 50:100, :] slices the part between pixels 100 and 200 in y (vertical) direction, and the part between pixels 50 and 100 in x (horizontal) direction.

See this working example:

import matplotlib.pyplot as plt

mydic = {
  "annotations": [
  {
    "class": "rect",
    "height": 98,
    "width": 113,
    "x": 177,
    "y": 12
  },
  {
    "class": "rect",
    "height": 80,
    "width": 87,
    "x": 373,
    "y": 43
  }
 ],
   "class": "image",
   "filename": "https://i.sstatic.net/9qe6z.png"
}


def crop(dic, i):
    image = plt.imread(dic["filename"])
    x0 = dic["annotations"][i]["x"]
    y0 = dic["annotations"][i]["y"]
    width = dic["annotations"][i]["width"]
    height = dic["annotations"][i]["height"]
    return image[y0:y0+height , x0:x0+width, :]


fig = plt.figure()
ax = fig.add_subplot(121)
ax.imshow(plt.imread(mydic["filename"]))

ax1 = fig.add_subplot(222)
ax1.imshow(crop(mydic, 0))

ax2 = fig.add_subplot(224)
ax2.imshow(crop(mydic, 1))

plt.show()

Upvotes: 2

Related Questions