Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Images > Image Slicing

Image Slicing

For the Python aficionados, it is also possible to do cropping by directly manipulating the two dimensional array of the image. Individual pixels could be extracted by treating the image like an array and specifying the (x, y) coordinates. Python can also extract ranges of pixels as well. For example, img[start_x:end_x, start_y:end_y] provides a cropped image from (start_x, start_y) to (end_x, end_y). Not including a value for one or more of the coordinates means that the border of the image will be used as the start or end point. So something like img[ : , 300:] works. That will select all of the x values and all of the y values that are greater than 300. In essence, any of Python’s functions for extracting subsets of arrays will also work to extract parts of an image, and thereby return a new image. Because of this, images can be cropped using Python’s slice notation instead of the crop function:

from SimpleCV import Image

img = Image('jacopo.png')

# Cropped image that is 200 pixels wide and 200 pixels tall starting at (50, 5).
cropImg = img[50:250,5:205]

cropImg.show()

Note

When using slice notation, specify the start and end locations. When using crop, specify a starting coordinate and a width and height.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint